How to parse a JSON array of objects in C#

后端 未结 2 1169
失恋的感觉
失恋的感觉 2020-12-22 14:11
{
   \"jsonstring\": [

  {
     \"id\":\"01\",
     \"language\": \"Java\",
     \"edition\": \"third\",
     \"author\": \"Herbert Schildt\"
  },

  {
     \"id\":         


        
相关标签:
2条回答
  • 2020-12-22 14:37

    In .NET you can use JavaScriptSerializer. First you will need to create a class that has the same structure as the json string.

    Example:

    {
        "Id":"07",
        "Language": "C++"
    }
    
    
    public class CSharpObject
    {
        int Id {get; set;}
        string Language {get; set;}
    }
    
    
    string json = HttpUtility.HtmlDecode(jsonString);
    JavaScriptSerializer json_serializer = new JavaScriptSerializer();
    CSharpObject csharpObject = (CSharpObject)json_serializer.Deserialize<CSharpObject>(json);
    
    0 讨论(0)
  • 2020-12-22 14:46

    Use NewtonSoft.NET:

    var obj = JsonConvert.DeserializeObject(json);
    

    Maybe make a corresponding class for using generics too:

    public class ClassName {
        public string id { get; set; }
        public string language { get; set; }
        public string edition { get; set; }
        public string author { get; set; }
    }
    

    then you can do:

    List<ClassName> list = JsonConvert.DeserializeObject<List<ClassName>>(json);
    
    0 讨论(0)
提交回复
热议问题