Deserialize JSON with C#

前端 未结 10 1454
一生所求
一生所求 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:10

    If you're using .NET Core 3.0, you can use System.Text.Json (which is now built-in) to deserialize JSON.

    The first step is to create classes to model the JSON. There are many tools which can help with this, and some of the answers here list them.

    Some options are http://json2csharp.com, http://app.quicktype.io, or use Visual Studio (menu EditPaste SpecialPaste JSON as classes).

    public class Person
    {
        public string Id { get; set; }
        public string Name { get; set; }
    }
    
    public class Response
    {
        public List<Person> Data { get; set; }
    }
    

    Then you can deserialize using:

    var people = JsonSerializer.Deserialize<Response>(json);
    

    If you need to add settings, such as camelCase handling, then pass serializer settings into the deserializer like this:

    var options = new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
    var person = JsonSerializer.Deserialize<Response>(json, options);
    
    0 讨论(0)
  • 2020-11-21 06:13

    You can use this extensions

    public static class JsonExtensions
    {
       public static T ToObject<T>(this string jsonText)
       {
           return JsonConvert.DeserializeObject<T>(jsonText);
       }
    
       public static string ToJson<T>(this T obj)
       {
           return JsonConvert.SerializeObject(obj);
       }
    }
    
    0 讨论(0)
  • 2020-11-21 06:15

    A great way to automatically generate these classes for you is to copy your JSON output and throw it in here:

    http://json2csharp.com/

    It will provide you with a starting point to touch up your classes for deserialization.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-21 06:16

    Serialization:

    // Convert an object to JSON string format
    string jsonData = JsonConvert.SerializeObject(obj);
    
    Response.Write(jsonData);
    

    Deserialization::

    To deserialize a dynamic object

      string json = @"{
          'Name': 'name',
          'Description': 'des'
        }";
    
    var res = JsonConvert.DeserializeObject< dynamic>(json);
    
    Response.Write(res.Name);
    
    0 讨论(0)
  • 2020-11-21 06:17

    I agree with Icarus (would have commented if I could), but instead of using a CustomObject class, I would use a Dictionary (in case Facebook adds something).

    private class MyFacebookClass
    {
        public IList<IDictionary<string, string>> data { get; set; }
    }
    

    or

    private class MyFacebookClass
    {
        public IList<IDictionary<string, object>> data { get; set; }
    }
    
    0 讨论(0)
提交回复
热议问题