1

So there is 1 website from where I need to get this JSON data. The link is direct (I am using the website's API )

THe problem is, the file is HUGE!. Tens of Thousands of Lines.. Even more..

I have visual studio 2013 and what I need to do is download that JSON data in a callback and then parse it to get a specific value. I am using Newtonsoft.JSON to parse it and here is what I have thought will be able to parse it

var obj = JsonConvert.DeserializeObject<JContainer>(jsonText);
var value = (int)obj["response"]["prices"]["5021"]["6"]["0"]["current"]["value"];

The problem is, how do I download all of that data and convert it into C# classes? Is there another way? Thanks a lot.

EDIT: If not JSON, I have option to download it in JSONP and VDF format

Here is the link of the JSON data - http://backpack.tf/api/IGetPrices/v3/?format=json&key=52f75dab4dd7b82f698b4568

Liam
  • 27,717
  • 28
  • 128
  • 190
Bone
  • 859
  • 2
  • 9
  • 17
  • I'm not convinced you should be sharing that key on the internet for everyone to see? – Liam Feb 10 '14 at 17:18
  • I don't think it is a problem.. I can anytime revoke it and get a new one – Bone Feb 10 '14 at 17:18
  • K, SO isn't letting me answer your question so... here: your thread is a duplicate of this resolved thread. http://stackoverflow.com/questions/4749639/deserializing-json-to-net-object-using-newtonsoft-or-linq-to-json-maybe – Brandon Feb 10 '14 at 17:19
  • Also this responds with the error message *You can only request this page once per minute per API key. Try again in 46 seconds.*.... – Liam Feb 10 '14 at 17:19
  • Liam, I belive you can understand what it means.. Try again later.. It can be accessed by someone only once per minute – Bone Feb 10 '14 at 17:19
  • Have you heard of the JavascriptSerializer? – Luke Alderton Feb 10 '14 at 17:20
  • No, not really. I don't have much experience in C# – Bone Feb 10 '14 at 17:21
  • 2
    @LukeAlderton What does it do better than Json.Net? – L.B Feb 10 '14 at 17:21
  • What's the actual question here? That file isn't huge by any strech. *What exactly are you having trouble with?* Json.Net is probably your best tool for this kind of work. even Micorsoft recommend it these days ahead of their (fundamentally flawed Javascriptserializer) – Liam Feb 10 '14 at 17:26
  • @Liam - How do I download it and parse the specific data I need.. I need this part ["response"]["prices"]["5021"]["6"]["0"]["current"]["value"]; – Bone Feb 10 '14 at 17:27
  • Um guys.. Thank you for your help and helping me with the basic data. @Brandon - I figured it out after reading a couple more questions. I have edited my main post with the answer included since I cannot post answer before 8 hours – Bone Feb 10 '14 at 17:50

3 Answers3

2

I got it working by doing this

using (var webClient = new System.Net.WebClient())
        {
            var json = webClient.DownloadString("http://backpack.tf/api/IGetPrices/v3/?format=json&key=00a00aaa0aa0a00a000a0000");
            Newtonsoft.Json.Linq.JObject o = Newtonsoft.Json.Linq.JObject.Parse(json);
            var value = (int)o["response"]["prices"]["5021"]["6"]["0"]["current"]["value"];
            Console.WriteLine(value);
        }

Thanks for your help everybody!!

Bone
  • 859
  • 2
  • 9
  • 17
0

Try restsharp. It let you do something like

var prices = client.Execute<Prices>(request);

Where Prices is the class that matches the returned schema

Ivo
  • 8,172
  • 5
  • 27
  • 42
0

Looking at the comment Brandon posted, he's right in principle, but you don't have to switch to Newtonsoft if you don't want. You just need to use a different JSON.NET API

var serializer = new JsonSerializer();
using (var stream = File.OpenRead("C:\\Users\\gweakliem\\Downloads\\sotest.js"))
{
    using (StreamReader streamReader = new StreamReader(stream))
    {
        using (JsonReader reader = new JsonTextReader(streamReader))
        {
            var aThing = serializer.Deserialize<JContainer>(reader);
            var aValue = (int) aThing["response"]["prices"]["5021"]["6"]["0"]["current"]["value"];
            Console.WriteLine("Read a value " + aValue);
        }
    }
}

If you're concerned about blocking on this thread while reading, it looks like you're going to have to write some code. I don't see awaitable methods on JsonTextReader or JsonSerializer, so I expect that those methods will block.

Now if you want to turn this into objects, here's a couple other SO posts:

Or this post covers a bunch of deserialization options.

Community
  • 1
  • 1
Gordon
  • 321
  • 1
  • 5