0

I'm trying to parse a Json that contains a lot of objects.

.json look like this: :

{
  "status" : "success",
  "prices" : [
    {
      "market_hash_name" : "4X4 Car",
      "price" : "7.87",
      "created_at" : 1472587613
    },
    {
      "market_hash_name" : "Yellow Car",
      "price" : "27.75",
      "created_at" : 1472519899
    }

[...] etc

and I just want to get the price of specific market hash name. How can I do that?

I have got this atm

using System.IO;
using Newtonsoft.Json;
      public class MarketHandler
        {
            //Methods
            public MarketHandler updatePrices()
            {
                var json = File.ReadAllText("PriceSkins.json");
                currentPrices = JsonConvert.DeserializeObject<Data>(json);
                return this;
            }

            public Data currentPrices { get; set; }
            public class Data
            {
                public Response response { get; set; }
            }

            public class Response
            {
                public string status { get; set; }
                public Price prices { get; set; }
            }

            public class Price
            {
                public string market_hash_name { get; set; }
                public string price { get; set; }
                public int created_at { get; set; }
            }
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
  • 1
    So what's the problem? Also, I don't see the code where you actually try to get the value. – user94559 Aug 31 '16 at 15:57
  • 2
    Your model doesn't match your JSON at the moment - you don't have a *single* price, you have an *array* of prices. (I'd urge you do follow .NET naming conventions and use attributes to specify the JSON representation, btw.) – Jon Skeet Aug 31 '16 at 15:57

1 Answers1

0

You can do like this, place your JSON some where else on your system and load the JSON like below

 Rootobject ro = new Rootobject();
 StreamReader sr = new StreamReader(Server.MapPath("text.json"));
 string jsonString = sr.ReadToEnd();
 JavaScriptSerializer ser = new JavaScriptSerializer();
 ro = ser.Deserialize<Rootobject>(jsonString);

Before that you have to create the classes like below these classes are matching your JSON, already I have answer similar this question here you can cehck how to create classes for JSON easily

public class Rootobject
{
    public string status { get; set; }
    public Price[] prices { get; set; }
}

public class Price
{
    public string market_hash_name { get; set; }
    public string price { get; set; }
    public int created_at { get; set; }
}

After that,from the instance of the Rootobject(ro) you can access the price like below

Price[] price_list = ro.prices;
Community
  • 1
  • 1
Shakir Ahamed
  • 1,290
  • 3
  • 16
  • 39