Coding Freak
Coding Freak

Reputation: 418

How to serialize a JsonArray with C#?

SO I have a Json array as following:

{[data, [{"name":"Micheal Jackson","pic_large":"https://scontent.x.fbcdn.net/v/t1.0-1/p200x200/14909900_10154513795037597_3241587822245799922_n.jpg?oh=54ead7e0ba74b45b632d96da1515ccf8&oe=591C4938","id":"10154729171332597"}

How can I serialize it with C# to parse it into objects and then pass it to the view.

EDIT:

{[data, [{"name":"Sayed Zubair Hashimi","pic_large":"https://scontent.xx.fbcdn.net/v/t1.0-1/p200x200/14909900_10154513795037597_3241587822245799922_n.jpg?oh=54ead7e0ba74b45b632d96da1515ccf8&oe=591C4938","id":"10154729171332597"},{"name":"Junaid Walizada","pic_large":"https://scontent.xx.fbcdn.net/v/t1.0-1/p200x200/14055012_1760562554217155_4937121194048198140_n.jpg?oh=376b49c9d04c2676ebe3d853b122165e&oe=58EA033D","id":"1821833754756701"},{"name":"Mohib Akhondzada","pic_large":"https://scontent.xx.fbcdn.net/v/t1.0-1/s200x200/14264218_592094647641140_6351146344336469735_n.jpg?oh=a8a63893d71f76c45fa3d07389f1700a&oe=59147C84","id":"648198542030750"},{"name":"Za Beah","pic_large":"https://scontent.xx.fbcdn.net/v/t1.0-1/p200x200/15741112_359701871054520_6692094260041596196_n.jpg?oh=6d9a0e73f70145b821c79cbe738090a0&oe=58E5B5B5","id":"360411140983593"},{"name":"Baser Nader","pic_large":"https://scontent.xx.fbcdn.net/v/t1.0-1/p200x200/15094436_10153876544626432_1550234361821853528_n.jpg?oh=e197fa712b3180a20612ecdacb01747c&oe=58E54DEC","id":"10153975726331432"},{"name":"Abasin Deniz","pic_large":"https://scontent.xx.fbcdn.net/v/t1.0-1/p200x200/15698075_440749809647293_7905213567074684088_n.jpg?oh=aeb22664f458d75fc00638ca6fa4ecfc&oe=591F7BB3","id":"444098429312431"}]]}

EDIT2: Here is how I retrieve above Json.

var appsecret_proof = access_token.GenerateAppSecretProof();

                var fb = new FacebookClient(access_token);

               dynamic myFeed = await fb.GetTaskAsync(
                    ("me/feed?fields=likes{{name,pic_large}}")
                    .GraphAPICall(appsecret_proof));

Upvotes: 0

Views: 844

Answers (2)

Cyber Progs
Cyber Progs

Reputation: 3873

first you need to create class with properties same as in json and then use the following code :

class obj
 {
   public string name {get ; set; }
   public string pic_large {get ; set; }
   public id {get ; set; }
 }

using System.Web.Script.Serialization;
.
.
var obj = new JavaScriptSerializer().Deserialize<obj>(jsonString);

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

The strings shown in your question are all invalid JSON. A properly formatted JSON might look like this:

{
    "data": [{
        "name": "Micheal Jackson",
        "pic_large": "https://scontent.x.fbcdn.net/v/t1.0-1/p200x200/14909900_10154513795037597_3241587822245799922_n.jpg?oh=54ead7e0ba74b45b632d96da1515ccf8&oe=591C4938",
        "id": "10154729171332597"
    }]
}

Now if you want to map this to C# class that's pretty easy to do. Just define the models to reflect this structure:

public class Feed
{
    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("pic_large")]
    public string PicLarge { get; set; }
}

public class Result
{
    [JsonProperty("data")]
    public IList<Feed> Feeds { get; set; }
}

and then all that's left is to deserialize the JSON string using a JSON serializer such as Json.NET back to this object structure:

string json = ... the json string shown above
var result = JsonConvert.DeserializeObject<Result>(json);
foreach (var feed in result.Feeds)
{
    Console.WriteLine(feed.Name);
}

Upvotes: 2

Related Questions