Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 474 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Deserialize JSON with C#

#1
I'm trying to deserialize a Facebook friend's [Graph API][1] call into a list of objects. The JSON object looks like:

{"data":[{"id":"518523721","name":"ftyft"},
{"id":"527032438","name":"ftyftyf"},
{"id":"527572047","name":"ftgft"},
{"id":"531141884","name":"ftftft"},
{"id":"532652067","name"...

List<EFacebook> facebooks = new JavaScriptSerializer().Deserialize<List<EFacebook>>(result);

It's not working, because the primitive object is invalid. How can I deserialize this?

[1]:

[To see links please register here]

Reply

#2
A great way to automatically generate these classes for you is to copy your JSON output and throw it in here:

[To see links please register here]


It will provide you with a starting point to touch up your classes for deserialization.
Reply

#3
Sometimes I prefer dynamic objects:

```
public JsonResult GetJson()
{
string res;
WebClient client = new WebClient();

// Download string
string value = client.DownloadString("https://api.instagram.com/v1/users/000000000/media/recent/?client_id=clientId");

// Write values
res = value;
dynamic dyn = JsonConvert.DeserializeObject(res);
var lstInstagramObjects = new List<InstagramModel>();

foreach(var obj in dyn.data)
{
lstInstagramObjects.Add(new InstagramModel()
{
Link = (obj.link != null) ? obj.link.ToString() : "",
VideoUrl = (obj.videos != null) ? obj.videos.standard_resolution.url.ToString() : "",
CommentsCount = int.Parse(obj.comments.count.ToString()),
LikesCount = int.Parse(obj.likes.count.ToString()),
CreatedTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds((double.Parse(obj.created_time.ToString()))),
ImageUrl = (obj.images != null) ? obj.images.standard_resolution.url.ToString() : "",
User = new InstagramModel.UserAccount()
{
username = obj.user.username,
website = obj.user.website,
profile_picture = obj.user.profile_picture,
full_name = obj.user.full_name,
bio = obj.user.bio,
id = obj.user.id
}
});
}

return Json(lstInstagramObjects, JsonRequestBehavior.AllowGet);
}
```
Reply

#4
Here is another site that will help you with all the code you need as long as you have a correctly formated JSON string available:

[To see links please register here]

Reply

#5
You need to create a structure like this:

public class Friends
{

public List<FacebookFriend> data {get; set;}
}

public class FacebookFriend
{

public string id {get; set;}
public string name {get; set;}
}

Then you should be able to do:

Friends facebookFriends = new JavaScriptSerializer().Deserialize<Friends>(result);

The names of my classes are just an example. You should use proper names.

Adding a sample test:

string json =
@"{""data"":[{""id"":""518523721"",""name"":""ftyft""}, {""id"":""527032438"",""name"":""ftyftyf""}, {""id"":""527572047"",""name"":""ftgft""}, {""id"":""531141884"",""name"":""ftftft""}]}";

Friends facebookFriends = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Friends>(json);

foreach(var item in facebookFriends.data)
{
Console.WriteLine("id: {0}, name: {1}", item.id, item.name);
}

Produces:

id: 518523721, name: ftyft
id: 527032438, name: ftyftyf
id: 527572047, name: ftgft
id: 531141884, name: ftftft
Reply

#6
I agree with Icarus (would have commented if I could),
but instead of using a **CustomObject** class,
I would use a **Dictionary** (in case Facebook adds something).

private class MyFacebookClass
{
public IList<IDictionary<string, string>> data { get; set; }
}

or

private class MyFacebookClass
{
public IList<IDictionary<string, object>> data { get; set; }
}

Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through