xxMattewxx
xxMattewxx

Reputation: 9

Deserializing a JSON file using C#

I'm creating a Steam APP ( For the Steam Platform ), and i need to deserialize a JSON file.

{
"response": {
    "success": 1,
    "current_time": 1401302092,
    "raw_usd_value": 0.245,
    "usd_currency": "metal",
    "usd_currency_index": 5002,
    "items": {
        "A Brush with Death": {
            "defindex": [
                30186
            ],
            "prices": {
                "6": {
                    "Tradable": {
                        "Craftable": [
                            {
                                "currency": "metal",
                                "value": 4,
                                "last_update": 1398990171,
                                "difference": 0.17
                            }
                        ]
                    }
                }
            }
        },
...

I just need to get Defindex and value. Already deserialized some simple JSON files, but i think this one is more complex.

For those who wants to know, I am using the API from BackpackTF...

Upvotes: 0

Views: 426

Answers (4)

tezromania
tezromania

Reputation: 817

This line of code will take your json as a string, and turn it into its root object.

RootObject obj = JsonConvert.DeserializeObject<RootObject>(jsonString);

The Json you provided is slightly flawed, but im guessing that the structure of c# objects you would be looking for would be close to this:

public class Craftable
{
    public string currency { get; set; }
    public int value { get; set; }
    public int last_update { get; set; }
    public double difference { get; set; }
}

public class Tradable
{
    public List<Craftable> Craftable { get; set; }
}

public class Prices
{
    public Tradable Tradable{ get; set; }
}

public class Items
{
    public List<int> defindex { get; set; }
    public Prices prices { get; set; }
}

public class Response
{
    public int success { get; set; }
    public int current_time { get; set; }
    public double raw_usd_value { get; set; }
    public string usd_currency { get; set; }
    public int usd_currency_index { get; set; }
    public Items items { get; set; }
}

public class RootObject
{
    public Response response { get; set; }
}

Upvotes: 0

mason
mason

Reputation: 32728

In general, what you want to do is making sure you have valid JSON (use JSON LINT for that), then get a C# class definition with Json2CSharp, then you will do something like this:

MyClass myobject=JsonConvert.DeserializeObject<MyClass>(json);

(We're assuming MyClass is based on what you got from Json2CSharp)

Then you access the values you want via the traditional C# dot notation.

Upvotes: 1

MoXplod
MoXplod

Reputation: 3852

Use NewtonSoft.Json And then you can use it as follows to get the data out.

    dynamic json = JsonConvert.DeserializeObject(<yourstring>);
    string currency = json.response.usd_currency;  // "metal"

Upvotes: 2

chfumero
chfumero

Reputation: 1147

Use a nuget package caller Newtonsoft.Json.5.0.8. it is on the nuget repository.

Upvotes: 0

Related Questions