简体   繁体   中英

Convert json data into datatable

I tried using json.net for converting json data into datatable, but am not able to solve, am newbie with json

code i tried:

 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"}}"

The class you're deserializing to must match the JSON string.

public class MyData
{
    public friends_info[] data { get; set; }
    public object paging { get; set; } // you can probably omit this
}

With that deserializing the given JSON string should work.

MyData myData = JsonConvert.DeserializeObject<MyData>(json);
friends_info finfo = myData.data;

Apart from that I'd very much suggest that you look into the C# basics again. Even that little code you posted violates the typical coding conventions.

im not sure but i think your string is not in correct format it should be like this.

"{'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 '}}";

if your json string is in correct format then @bstenzel suggestion should work fine. Thanx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM