asp.net mvc - How to Get Values from a json response string in C# -
asp.net mvc - How to Get Values from a json response string in C# -
i want "id" "gender", "name", "picture" json response string
{ "data": [{ "name": "xxx", "gender": "male", "id": "528814", "picture": { "data": { "is_silhouette": false, "url": "https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-frc3\/v\/t1.0-1\/p50x50\/551182_10152227358459008__n.jpg?oh=983b70686285c2f60f71e665ace8ed5f&oe=54c1220c&__gda__=1422017140_998fbe013c4fe191ccadfdbc77693a76" } } } string[] info = friendsdata.split(new string[] { "}," }, stringsplitoptions.removeemptyentries).toarray(); foreach (string d in data) { seek { facebookfriend f = new facebookfriend { id = d.substring("\"id\":\"", "\""), gender = d.substring("gender\":\"", "\""), name = d.substring("name\":\"", "\""), image = d.substring("\"picture\":{\"data\":{\"url\":\"", "\"").replace(@"\", string.empty) }; facebookfriendlist.add(f); } grab { continue; } }
this code looks bad if json info changes need modify logic accordingly. suggest serialize , deserialize model info using json serialization.
model:
public class serializationmodel { public info data { get; set; } } public class info { public string name { get; set; } public string gender { get; set; } public string id { get; set; } public image picture { get; set; } } public class image { public picturedata info { get; set; } } public class picturedata { public bool is_silhouette { get; set; } public string url { get; set; } }
serialize info json output like,
serializationmodel serializationmodel = new serializationmodel { info = new info { gender = "male", id = "88", name = "user", image = new image { info = new picturedata { is_silhouette = true, url = "www.google.com" } } } }; string serializedstring = newtonsoft.json.jsonconvert.serializeobject(serializationmodel);
which yield below result,
{"data":{"name":"user","gender":"male","id":"88","picture":{"data":{"is_silhouette":true,"url":"www.google.com"}}}}
to deserialize json info model,
serializationmodel sm = newtonsoft.json.jsonconvert.deserializeobject<serializationmodel>(serializedstring);
then can required values model itself.
c# asp.net-mvc json
Comments
Post a Comment