Removing JSON deserialization into an array of strings

I just started doing C #, and I've been knocking on JSON deserialization for a few seconds now. I am using the Newtonsoft.Json library. I expect only the json response of the array of dictionaries per se

[{"id":"669","content":" testing","comments":"","ups":"0","downs":"0"}, {"id":"482","content":" test2","comments":"","ups":"0","downs":"0"}]

Now I have: (note: loading is just a string containing a json string)

string[] arr = JsonConvert.DeserializeObject<string[]>(download);

I tried many different ways to do this, each failed. Is there a standard way for parsing json of this type?

+5
source share
2 answers

You have an array of objects , not strings. Create a class that displays properties and deserializes into it,

public class MyClass {
    public string id { get; set; }
    public string content { get; set; }
    public string ups { get; set; }
    public string downs { get; set; }
}

MyClass[] result = JsonConvert.DeserializeObject<MyClass[]>(download);

JSON , . , , .. http://www.json.org/ http://www.w3schools.com/json/default.asp - . , JSON :

["One", "Two", "Three"]
+12

, .

 var jsonResponse = 
  [{"Id":2,"Name":"Watch"},{"Id":3,"Name":"TV"},{"Id":4,"Name":""}]

 var items = JsonConvert.DeserializeObject<List<MyClass>>(jsonResponse);

MyClass -

 public class MyClass
            {
                public int Id { get; set; }
                public string Name { get; set; }
            }
0

All Articles