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:
  • 316 Vote(s) - 3.4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I connvert a JSON object to a custom C# object?

#11
A good way to use JSON in C# is with [JSON.NET][1]

[Quick Starts & API Documentation][2] from [JSON.NET - Official site][3] help you work with it.

An example of how to use it:

public class User
{
public User(string json)
{
JObject jObject = JObject.Parse(json);
JToken jUser = jObject["user"];
name = (string) jUser["name"];
teamname = (string) jUser["teamname"];
email = (string) jUser["email"];
players = jUser["players"].ToArray();
}

public string name { get; set; }
public string teamname { get; set; }
public string email { get; set; }
public Array players { get; set; }
}

// Use
private void Run()
{
string json = @"{""user"":{""name"":""asdf"",""teamname"":""b"",""email"":""c"",""players"":[""1"",""2""]}}";
User user = new User(json);

Console.WriteLine("Name : " + user.name);
Console.WriteLine("Teamname : " + user.teamname);
Console.WriteLine("Email : " + user.email);
Console.WriteLine("Players:");

foreach (var player in user.players)
Console.WriteLine(player);
}




[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply

#12
Using `JavaScriptSerializer()` is less strict than the generic solution offered :

public static T Deserialize<T>(string json)

That might come handy when passing json to the server that does not match exactly the Object definition you are trying to convert to.
Reply

#13
Rather than sending as just an object, create a public class of properties that is accessible and send the data to the Webmethod.

[WebMethod]
public static void SaveTeam(useSomeClassHere user)
{
}

Use the same parameters names in the Ajax call to send the data.



Reply

#14
Another *really* simple solution is using the library Newtonsoft.Json:

User user = JsonConvert.DeserializeObject<User>(jsonString);

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

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