Deserialize JSON with C#

前端 未结 10 1457
一生所求
一生所求 2020-11-21 05:57

I\'m trying to deserialize a Facebook friend\'s Graph API call into a list of objects. The JSON object looks like:

{\"data\":[{\"id\":\"518523721\",\"name\"         


        
10条回答
  •  北海茫月
    2020-11-21 06:16

    Newtonsoft.JSON is a good solution for these kind of situations. Also Newtonsof.JSON is faster than others, such as JavaScriptSerializer, DataContractJsonSerializer.

    In this sample, you can the following:

    var jsonData = JObject.Parse("your JSON data here");
    

    Then you can cast jsonData to JArray, and you can use a for loop to get data at each iteration.

    Also, I want to add something:

    for (int i = 0; (JArray)jsonData["data"].Count; i++)
    {
        var data = jsonData[i - 1];
    }
    

    Working with dynamic object and using Newtonsoft serialize is a good choice.

提交回复
热议问题