0

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...

4
  • 1
    Does this help? json2csharp.com Commented May 28, 2014 at 20:00
  • 3
    The JSON you posted has a syntax error. The Object for "items" can't immediately contain another Object without specifying a key for it. Was that meant to be an Array containing Objects? Commented May 28, 2014 at 20:02
  • I've tried using other topics at stackoverflow too, one of them never gived me an error or crash, but always returned 0. Basically that is the BackPack.tf API, so the reason i'm using it is because i need the prices. I know alot of C#, but i'm kinda newbie to deserialization :l Commented May 28, 2014 at 20:22
  • Lol thanks @Ahmedilyas, it worked the way it's needed. Commented May 28, 2014 at 20:38

4 Answers 4

2

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"
Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

It's the API from a famous website to Steam users, and as there are alot programs that use, i'm sure it is a valid fie.
Regardless of whether it's valid or not, you want to always check because you never know! So check when you create your class structure to deserialize it to, and then at runtime always wrap it in a try/catch to make handle if it is invalid. Don't assume things are valid.
0

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

5 Comments

+1 for Json.NET. Here's a link to the website for it: james.newtonking.com/json
-1 While most of us wholeheartedly recommend JSON.NET, the question was already tagged with JSON.NET. So he's probably using it and doesn't know how. Instead of just saying "use JSON.NET" you should explain how to use it to accomplish this task.
As is, this would be a better fit as a comment. It may be good advice, but doesn't really provide an answer. And, with the up-and-downs the have occurred, you now have enough rep to post that comment.
He already is using json.net, but is asking for implementation on how to use it.
Yeah. I'm using Json.net, but i'm newbie to it. Have experience with coding at all, but i'm new to deserialization methods..
0

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; }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.