我尝试使用json.net将json数据转换为数据表,但无法解决,我是json 的新手
我尝试过的代码:
string json = JsonConvert.SerializeObject(friend);
friends_info finfo = JsonConvert.DeserializeObject<friends_info>(json);
public class friends_info
{
public friends_info()
{
}
public string name_; // Backing field
public string name
{
get { return name_; } // Getter
set { name_ = value; } // Setter
}
public string id_; // Backing field
public string id
{
get { return id_; } // Getter
set { id_ = value; } // Setter
}
}
where string json="{"data":[{"name":"Angelina Jovy","id":"100000599264453"},{"name":"Luvbhie Rose May Aviles","id":"100001102845189"},{"name":"Nainy Ahuja","id":"100001103300515"},{"name":"Sabrina Reis","id":"100008357430263"}],"paging":{"next":"https://graph.facebook.com/v1.0/1539545690/friends?access_token=CAAGjOBYUDq0BAAFnIgfyfvMftE1ImSEfZCK7R7NdFYw5lnKuddHwqqlm20DTuZCjEeUh2hzMD0KAJpY1ozq3aPuh9nQUHBrXtG0Qu2sd6RwotUQtYj9jtGcMlJEzZCCBLLH8CZBSNQIZAzC2ASOxkYf3JCfwGZA7XSzF5y2iPVDWRCfrl8C4rZAZBzkJiaJwytVvSintYLRfySaunO81fAei&limit=5000&offset=5000&__after_id=enc_AewuVVCxM4Iz1IuazCHob3SZku3BDZ6NeU054UtCU_gc0QDAm2g2VNM__lcbuJNDtm9RmHLU-QCQifFun9H__Zqs"}}"
要反序列化的类必须与JSON字符串匹配。
public class MyData
{
public friends_info[] data { get; set; }
public object paging { get; set; } // you can probably omit this
}
这样一来,反序列化给定的JSON字符串就可以工作了。
MyData myData = JsonConvert.DeserializeObject<MyData>(json);
friends_info finfo = myData.data;
除此之外,我非常建议您重新研究C#的基础知识。即使是你发布的那些小代码也违反了典型的编码惯例。
我不确定,但我认为您的字符串格式不正确,应该是这样的。
"{'数据':[{'名称':'安吉利娜·乔维','id':'100000599264453'},{'姓名':'露比·罗斯·梅·阿维莱斯','i':'100001102845189'};{'名字':'奈尼·阿胡贾','d':'10001103300515'}、{'名':'萨布丽娜·赖斯','id':'1100008357430263'}],'老化':{'下一个':'https://graph.facebook.com/v1.0/1539545690/friends?access_token=CAAGjOBYUDq0BAAFnIgfyfvMftE1ImSEfZCK7R7NdFYw5lnKuddHwqqlm20DTuZCjEeUh2hzMD0KAJpY1ozq3aPuh9nQUHBrXtG0Qu2sd6RwotUQtYj9jtGcMlJEzZCCBLLH8CZBSNQIZAzC2ASOxkYf3JCfwGZA7XSzF5y2iPVDWRCfrl8C4rZAZBzkJiaJwytVvSintYLRfySaunO81fAei&limit=5000&偏移=5000&__after_id=enc_AewuVVCxM41IzuazCHob3SZku3BDZ6NeU054UtCU_gc0QDAm2g2VN__lcbuJNDtm9RmHLU-QCQifFun9H__Zqs'}";
如果json字符串的格式正确,那么@bstenzel建议应该可以正常工作。Thanx