0

Below is my JSON structure:

{
  "status": "success",
  "data": {
    "MyValues": [
      [
        "2018-09-06T09:15:00+0530",
        1030,
        1038.75,
        1017.2,
        1030.9,
        542542
      ],
      [
        "2018-09-07T09:15:00+0530",
        1032.7,
        1035.45,
        1015.5,
        1025.35,
        410461
      ]
    ]
  }
}   

I am using Newtonsoft JSON. To make it strongly typed, I created below classes, considering JSON structure:

class MyValues
        {
            public DateTime TimeStamp { get; set; }
            public decimal First { get; set; }
            public decimal Second { get; set; }
            public decimal Third { get; set; }
            public decimal Fourth { get; set; }
            public decimal Fifth { get; set; }
        }

  class Data
        {
            public MyValues[] MyValues { get; set; }
        }

  class MyData
        {
            public string  Status { get; set; }

            public Data Data { get; set; }
         }

Finally, Below is the code I have written. It reads the above json object from jd.txt file and tries to parse it:

using (StreamReader file = File.OpenText(@"jd.txt"))
 {
   Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer();
    MyData MyData = (MyData)serializer.Deserialize(file, typeof(MyData));
  }

When I run above code, I see MyData.Data.MyValues null. I am unable to figure out the problem.
Kindly guide me to solve the problem

Aditya Bokade
  • 1,708
  • 1
  • 28
  • 45
  • 1
    My best suggestion: Create an object using your classes and Serialize it `to` JSON. Your data is an array but you have a class with a bunch of properties. You can look at the serialized output to see how to structure your data. – No Refunds No Returns Sep 08 '18 at 13:13
  • 2
    There are no properties named First, Second etc in the JSON – Ňɏssa Pøngjǣrdenlarp Sep 08 '18 at 13:15
  • Admins, what could be the reason of down-voting this question? – Aditya Bokade Sep 08 '18 at 14:00
  • 1
    You need to use a converter to map the inner array values from the JSON into your `MyValues` class. See [How to deserialize a JSON array into an object using Json.Net?](https://stackoverflow.com/q/47556191/10263). In fact, I think this question is essentially a duplicate of that one. – Brian Rogers Sep 08 '18 at 14:06
  • Another duplicate (or near duplicate): [Read JSON where properties are in nested array instead of properties of an object](https://stackoverflow.com/a/39462464/3744182). – dbc Sep 08 '18 at 15:03

2 Answers2

1

Your model should be:

public class Data
{
    public List<List<object>> MyValues { get; set; }
}

public class MyData
{
    public string status { get; set; }
    public Data data { get; set; }
}

And then serialize and access to the datas:

using (StreamReader file = System.IO.File.OpenText(@"jd.txt"))
    {
       Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer();
       MyData MyData = (MyData)serializer.Deserialize(file, typeof(MyData));
       return MyData.data.MyValues[0][0].ToString();
     }
Yanga
  • 2,885
  • 1
  • 29
  • 32
  • Admins, I understand that its a duplicate-alike question. However, I was unable to map the JSON structure to Model. Particularly I was confused about List> MyValues.Thanks @Yanga For help. God bless! – Aditya Bokade Sep 11 '18 at 15:57
0

1.)Create a Mydata object and load allvariables

2.)serialize it(if it is web service output you can observe json string your in browser)

3.)according to serialized format of json you can figure out what kind of json it accepts in return.

4.)after finding json it expects you can construct that kind of json,so that it can deserialize it.

5.)For example :

Mydata object json string(which we call serialized Mydata) is like this:

{
      "status":"active",
       "data":{
               "MyValues":[
                            {
                              "Name":"k",
                               "ID":"122"
                            },
                            {
                             "Name":"a",
                             "ID":"123"
                            }
                         ]
              }
 }

then your class expects this format of json string only to convert it back to Mydata object( which we call deserialization)

Chandra
  • 156
  • 3
  • 11