1

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"}}"
2
  • Your json is nothing like your class, you could serliase/desialise the elements of the "data" array in your json into/from friends_info classes but not the whole string into/from a single instance. Sep 18, 2014 at 10:54
  • @BenRobinson: thanks for reply, my class friends_info has name,id which i want to fetch from json data, Sep 18, 2014 at 11:01

2 Answers 2

2

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.

0
0

i m 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.