Zvrty
Zvrty

Reputation: 13

How do I parse JSON data which contains only arrays and nested arrays with no property names?

So I've looked around for tutorials on this and all the tutorials I've found doesn't have JSON that looks like the one I'm trying to parse.

I'm trying to parse JSON from this website https://www.steamcardexchange.net/api/request.php?GetBadgePrices_Guest

Since it doesn't have any identifiers for each thing like name, id etc I'm not sure how I will go about extracting data from it.

My only interest is really getting the first number of each item

[["449940","! That Bastard Is Trying To Steal Our Gold !"],5,"$0.64","1519294200"]

so what I want to extract from this item would be "449940".

This is what I've got so far

using (var client = new WebClient())
        {
            client.DownloadFile("https://www.steamcardexchange.net/api/request.php?GetBadgePrices_Guest", "data.json");
        }


        using (StreamReader r = new StreamReader("data.json"))
        {
            string json = r.ReadToEnd();

            //Parse somehow
        }

Any tips?

Upvotes: 1

Views: 174

Answers (1)

Scott Hannen
Scott Hannen

Reputation: 29302

I took this up out of sheer curiosity because I had no idea how to parse this either. Perhaps there's a much better way.

I started by pasting a fragment of this into json2csharp.com.

The class it generates is

public class RootObject
{
    public List<List<object>> data { get; set; }
}

From there I wrote some classes that correspond to what I think the data is supposed to look like. The names of the classes and properties are meaningless, so change them to whatever these actually represent.

public class OutputItem
{
    public Message Message { get; set; }
    public long Int64Value { get; set; }                     // 5
    public string StringThatLooksLikeCurrency { get; set; }  // "$0.64"
    public string StringThatLooksNumeric { get; set; }       // "1519294200"
}

public class Message
{
    public string MessageId { get; set; }                    // "449940"
    public string MessageText { get; set; }                  // "! That Dude..."
}

And finally, some sample code that takes a fragment of that JSON and converts it to a list of OutputItem. In order to figure this out I first deserialized the JSON to RootObject, then I inspected the deserialized object in the debugger to make sense of what it looked like.

var json = @"{ ""data"": [[[ ""449940"", ""! That Dude Is Trying To Steal Our Gold !"" ], 5, ""$0.64"", ""1519294200"" ], [[ ""303720"", ""#killallzombies"" ], 5, ""$0.56"", ""1519322799"" ]]}";
var parsed = JsonConvert.DeserializeObject<RootObject>(json);
var outputItems = new List<OutputItem>();
foreach (var listOfObject in parsed.data)
{
    var outputItem = new OutputItem();
    var message = (JArray) listOfObject[0];
    outputItem.Message = new Message {MessageId = (string) message[0], 
        MessageText = (string) message[1]};
    outputItem.Int64Value = (long) listOfObject[1];
    outputItem.StringThatLooksLikeCurrency = (string) listOfObject[2];
    outputItem.StringThatLooksNumeric = (string) listOfObject[3];
    outputItems.Add(outputItem);
}

Upvotes: 2

Related Questions