How do I convert a JSON String for better use in C#

242 Views Asked by At

I got this lists here (http://api.steampowered.com/ISteamApps/GetAppList/v0001/ or http://api.steampowered.com/ISteamApps/GetAppList/v2/) which contain App-ID's and the associated names. I don't know how to convert these strings to an array or something. I want to make a function which returns the Name of a given App-ID.

I tried everything.. How do I convert these right?

4

There are 4 best solutions below

4
On BEST ANSWER

you can create a few C# classes to serve you when you deserialize the JSON data coming from your links. consider the following code:

public class App
{
    public int appid { get; set; }
    public string name { get; set; }
}

public class Applist
{
    public List<App> apps { get; set; }
}

public class RootObject
{
    public Applist applist { get; set; }
}

now as you have some c# classes to map your JSON to, you only need to deserialize the incoming JSON data to the RootObject class, and then you will have your list so you can query it with your C# skills.

example of how to deserialize JSON in C#

    // Assuming that result is the incoming JSON object
     RootObject steamRootObject = 
                        new JavaScriptSerializer().Deserialize<RootObject>(result);

    // now you have your data inside steamRootObject, 
    // loop over the data and extract whatever fields you want.

    foreach(var app in steamRootObject.Applist.apps)
    {
       // your logic goes here
    }

Note: I've created the classes using this website: http://json2csharp.com/

2
On

If I understood correctly your question, here a tutorial to parse a JSON string in a object. There is some code to copy; this code creates a class you can you to reach the goal.

If it isn't what you need, please clarify your needs.

0
On

First, you will need a library to automaticaly translate your json into objects. I recomend you the Json.NET (Newtonsoft), one of the best libs in my opinion.

After you installed it using Nuget, you will need to build your models to represent this JSON, if your are too lazy to make it by yourself, I recommend you to check ou the website http://json2csharp.com/, you put your JSON or URL, and they generate the models for you.

After that, some changes will be necessarie for a simple aproach, first anotate every class involved on your request with [DataContract] and every property with [DataMember(Name = "PROPERTY NAME")] to help Json.NET find your JSON properties. This way, you will help the library serializa/deserialize the data for you, and you will have an easy, fast and reliable translation from JSON String to your object. Using the DataMember anotation, you don't need to restrain yourself from using the same name from your json property to your class.

Just to help you out, a little example using the class App

[DataContract]
public class App
{
    [DataMember(Name = "appid")]
    public int Appid { get; set; }
    [DataMember(Name = "name")]
    public string Name { get; set; }
}

PS: To Use DataContract and DataMember, you need to add a reference for System.Runtime.Serialization

0
On

Try with below link:

Fiddle for json parsing