0

Yesterday was first day when i met JSON and most of example that i find was where JSON have format like

{ "key1": "value1", "key2": "value2", ... }

but i have json string:

{"items":[[24,68,216,34006,32224],[68,177,277,140,2130], |...skip big amount of data....| ,[79606,8500392,0,0,14]],"updated":1475686082000,"columns":["id","buy","sell","supply","demand"]}

I trying to figure out how to read this and get specific amount of data. As example i need to get number in column "buy" and "sell" of specific IDs.

5
  • json2csharp.com Commented Oct 5, 2016 at 18:00
  • Are you deserializing the object or were you looking to use an XmlReader type thing (except for JSON)? If you deserialize it you can just use LINQ. Commented Oct 5, 2016 at 18:01
  • Can you provide some piece of your code where you deserialize that JSON? Commented Oct 5, 2016 at 18:05
  • 1
    As far as reading it goes, anything contained inside [ ] is considered an array. So the "items" field is an array containing arrays Commented Oct 5, 2016 at 18:05
  • @L.B. thanks for site Commented Oct 5, 2016 at 18:28

2 Answers 2

2

According to your json your model should be

public class YourRootObject
{
    public List<List<int>> items { get; set; }
    public long updated { get; set; }
    public List<string> columns { get; set; }
}

and now you can deserialize as

var obj = JsonConvert.DeserializeObject<YourRootObject>(json);

But, Instead of dealing with "updated" value later, I would change the model as below and write a json converter.

public class YourRootObject
{
    public List<List<int>> items { get; set; }
    public DateTime updated { get; set; }
    public List<string> columns { get; set; }
}

public class EpochToDatetimeConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(DateTime);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var l = (long)reader.Value;
        return new DateTime(1970, 1, 1).AddMilliseconds(l).ToLocalTime();
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

Now you can deserialize as

var obj = JsonConvert.DeserializeObject<YourRootObject>(json, 
                                           new EpochToDatetimeConverter());
Sign up to request clarification or add additional context in comments.

2 Comments

Updated value not really date itself, it's more like serial number
@Dante4 Maybe, but it returns 5th of Oct. 2016 19:48:02. Just a considense?
0

As @Alex has said in the comments:

As far as reading it goes, anything contained inside [ ] is considered an array. So the "items" field is an array containing arrays

If you don't want to have this array of arrays, you can create a new class to encapsulate the inner array. For instance:

{
  "items":
    [
      { "numbers": [24,68,216,34006,32224] },
      { "numbers": [68,177,277,140,2130] }, 
      |...skip big amount of data....| ,
      { "numbers": [79606,8500392,0,0,14]],
    ],
  "updated": 1475686082000,
  "columns": [ "id", "buy", "sell", "supply", "demand"]
}

And your classes in C# would be:

public class Parent
{
  public List<Child> items;
  public int updated;
  public string[] columns;

  public Parent()
  {
    items = new List<Child>();
  }
}

public class Child
{
  public string[] numbers;
}

Something like that.

4 Comments

Thanks for point this, i guess i still doing something wrong, because i got NULL in Parent.items, i tried to do this "Parent ItemsNew = JsonConvert.DeserializeObject<Parent>(File.ReadAllText(@"C:\\items.json"));
Can you try using a constructor inside Parent class? public Parent() { items = new List<Child>(); } I'll update my answer if that works.
@"Rick Wolff" Error "Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'WindowsFormsApplication1.Form1+Child' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly...Path 'items[0]', line 1, position 11."
Have you updated your JSON to match the structure I showed in the begginig of my answer? items > numbers

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.