Moving from gLUA to C# | json.decode into table, equivalent for C#?
3 replies, posted
Hi all,
So I'm currently learning C# from a strong background of gLUA and I've ran into a problem.
I've successfully fetched a json string from a url however I am unsure how to decode it, since I'm used to using json.decode and putting it into table form however I've been told to use a library like Newtonsoft JSON.
How would I go about converting json into a object?(or correct me on what it's called) when there is more arrays inside of each entry
Thanks,
Computer600
If you go with Newtonsoft JSON you have two options.
First is creating a class to hold the data and de-serialising straight to that with
[code]YourClass object = Newtonsoft.Json.JsonConvert.DeserializeObject<YourClass>(jsonString);[/code]
Second is to just parse it into a JToken and access the elements by name.
[code]Newtonsoft.Json.Linq.JToken token = Newtonsoft.Json.Linq.JObject.Parse(JSON);
int value = (int)token["name"];[/code]
Just replace int with the correct datatype per member. The Select method of a JToken could be useful here.
I personally prefer the first one but the second allows you to change the data names (cid in JSON to CategoryID in C# for example).
If you're lazy there's a site that generates classes from JSON:
[url]http://json2csharp.com/[/url]
[QUOTE=Rohans;49779692]If you're lazy there's a site that generates classes from JSON:
[url]http://json2csharp.com/[/url][/QUOTE]
If you're super lazy you can do it straight from Visual Studio (though you might need ASP.NET tooling installed?)
[url]https://www.matthewproctor.com/json-to-c-sharp-class-using-paste-special/[/url]
Sorry, you need to Log In to post a reply to this thread.