我以下面提到的格式获得json。
[{
"count":"2",
"data":[{
"Name": "Pinky",
"number": "91XXXX",
"place": "Mumbai"
},
{
"Name": "Rohan",
"number": "91XXXX",
"place": "Pune"
}]
}]
对于练习,我把它变成了像这样的字符串
string json = "[{
\"count\":\"2\",
\"data\":[{
\"Name\": \"Pinky\",
\"number\": \"91XXXX\",
\"place\": \"Mumbai\"
},
{
\"Name\": \"Rohan\",
\"number\": \"91XXXX\",
\"place\": \"Pune\"
}]
}]";
我为此创建了一个类。
public class Datum
{
public string Name { get; set; }
public string number { get; set; }
public string place { get; set; }
}
public class RootObject
{
public string count { get; set; }
public List<Datum> data { get; set; }
}
但是当我这样写作时,会收到错误。
RootObject cdr = JsonConvert.DeserializeObject<RootObject>(json);
答案 0 :(得分:2)
你的json是一个数组使用
var cdr = JsonConvert.DeserializeObject<List<RootObject>>(json);