1

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?

Scott Hannen
  • 27,588
  • 3
  • 45
  • 62
Zvrty
  • 13
  • 4
  • Why download a file? Just make a GET request to that endpoint – maccettura Feb 22 '18 at 20:40
  • Have you ever heard about JSON.NET and JsonTextReader? – Kevin Avignon Feb 22 '18 at 20:40
  • 1
    Use JSON.Net and structure your class accordingly. Looks like you would need a class that holds a list called "Data".. Try and be more specific with your naming conventions. – Brandon Miller Feb 22 '18 at 20:43
  • 1
    And `HttpClient` – Matthew Whited Feb 22 '18 at 20:43
  • @maccettura I tried with HtmlWeb() but whenever i use that it doesnt load the page like in browse, instead it attempts to make me download the file so I use this instead. – Zvrty Feb 22 '18 at 20:43
  • @Zvrty use [HttpClient](https://stackoverflow.com/questions/10928528/receiving-json-data-back-from-http-request) and [JSON.NET](http://json.net). Also make sure you are [using HttpClient correctly](https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/) – maccettura Feb 22 '18 at 20:44
  • @maccettura Thank you, it worked just fine with HttpClient. But I don't know how I can parse it using JSON.NET since most tutorials mention DeserializeObject but since it doesn't have any properties just pure data what should I use instead? – Zvrty Feb 22 '18 at 20:53
  • @Zvrty I am not following. You should be able to use `JsonConvert.DeserializeObject(someJson);`. You will need to create C# classes that represent your data. See [this question](https://stackoverflow.com/questions/11260631/convert-json-into-class-object-in-c-sharp) – maccettura Feb 22 '18 at 20:54
  • @maccettura Yes I have created a class for it but if you look at that question they have things like err_code, flight_date etc and in my case there is no things like that just the values. – Zvrty Feb 22 '18 at 20:59
  • Use [JsonProperty attributes](https://stackoverflow.com/a/8796648/2457029) if your c# property name is different from the JSON property – maccettura Feb 22 '18 at 21:01
  • but the problem is there is no JSON properties at all in my case so it doesnt know what to look for. – Zvrty Feb 22 '18 at 21:06
  • [Json.NET Serialization Guide](https://www.newtonsoft.com/json/help/html/SerializationGuide.htm) – Heretic Monkey Feb 22 '18 at 21:08
  • Oh, and I followed your link, and in fact, there is an object, with a "data" property which contains the data you've shown, which is an array that contains an array as one of its elements. In any case, look into [`JObject.Parse`](https://www.newtonsoft.com/json/help/html/Overload_Newtonsoft_Json_Linq_JObject_Parse.htm) and [`JArray.Parse`](https://www.newtonsoft.com/json/help/html/Overload_Newtonsoft_Json_Linq_JArray_Parse.htm) – Heretic Monkey Feb 22 '18 at 21:13
  • Possible duplicate of [Parsing through JSON in JSON.NET with unknown property names](https://stackoverflow.com/questions/14714085/parsing-through-json-in-json-net-with-unknown-property-names) – Heretic Monkey Feb 22 '18 at 21:15

1 Answers1

2

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);
}
Scott Hannen
  • 27,588
  • 3
  • 45
  • 62