Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
153 views
in Technique[技术] by (71.8m points)

How to pull down an schema-less JSON document from CosmosDB in Xamarin App

I am creating an app in Xamarin and am having issues querying a general JSON document from my CosmosDB. I am able to query my DB with a known structure (very similar to what we see in the Xamarin ToDo Example):

public async static Task<List<ToDoItem>> GetToDoItems()
{
var todos = new List<ToDoItem>();

if (!await Initialize())
    return todos;

**var todoQuery = docClient.CreateDocumentQuery<ToDoItem>(
    UriFactory.CreateDocumentCollectionUri(databaseName, collectionName),
    new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = true })
    .Where(todo => todo.Completed == false)
    .AsDocumentQuery();**

while (todoQuery.HasMoreResults)
{
    var queryResults = await todoQuery.ExecuteNextAsync<ToDoItem>();

    todos.AddRange(queryResults);
}

return todos;
}

The problem I see with this "code fixed scheme" approach is that if the scheme of your JSON file changes throughout development, older versions of code will overwrite the newer scheme in CosmosDB since writes to the DB are on a document level and not a property level. Instead, it would be helpful for older versions of the code to be able to pull down the latest scheme and work with the properties that it knows about without having to force the user to update.

Does anyone know how to query a schema-less JSON document out of CosmosDB? Thanks!

question from:https://stackoverflow.com/questions/66054513/how-to-pull-down-an-schema-less-json-document-from-cosmosdb-in-xamarin-app

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use JObject as the type to query a schema-less JSON document out of CosmosDB, and use following code to pull the raw JSON data into your object:

JsonConvert.DeserializeObject<Class>(JSONString);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...