0

I have created JSON file which contains like as below

[
  {
    "points": [
      {
        "index": 0,
        "AccX": -0.0699
      },
      {
        "index": 1,
        "AccX": -0.0605
      },
      "name": "AccX",
    "Calibration": [
      {
        "Bias": "0.0",
        "Gain": "1.0"
      }
    ],
    "MinAttr": -0.0028,
    "MaxAttr": 3.1936
  },
  {
    "points": [
      {
        "index": 0,
        "AccY": -0.0699
      },
      {
        "index": 1,
        "AccY": -0.0605
      },
      "name": "AccY",
    "Calibration": [
      {
        "Bias": "0.0",
        "Gain": "1.0"
      }
    ],
    "MinAttr": -0.0028,
    "MaxAttr": 3.1936
  }

]

My Model class to get the object for this json data is -

 public class Points
    {
        public int index { get; set; }
        public int AccX { get; set; }

        public int AccY { get; set; }

        public int AccZ { get; set; }

        public int gyroZ { get; set; }

    }

    public class RootObject
    {
        public List<Points> point { get; set; }
    }

in control class i have used like this,

 var ReadJson = System.IO.File.ReadAllText(Server.MapPath(@"~/App_Data/chartData.json"));
            JavaScriptSerializer ser = new JavaScriptSerializer();

            ser.MaxJsonLength = int.MaxValue;
            RootObject json = ser.Deserialize<RootObject>(ReadJson);

            foreach (var item in json.point)
            {

                ViewBag.Name = item.index;
                ViewBag.ShortText = item.AccX;
            }

        return View();

I hope i am doing this wrong i need to deserialize the json data and return data...

how to achieve this?

Akbar Basha
  • 1,168
  • 1
  • 16
  • 38

1 Answers1

1

At first, check your JSON correctly ? (Just checking it via Json Parser Online)

I think your JSON may be like this:

[
  {
    "points": [
      {
        "index": 0,
        "AccX": -0.0699
      },
      {
        "index": 1,
        "AccX": -0.0605
      }],
        "name": "AccX",
        "Calibration": [{
          "Bias": "0.0",
          "Gain": "1.0"
        }],
      "MinAttr": -0.0028,
      "MaxAttr": 3.1936
      },
  {
    "points": [
      {
        "index": 0,
        "AccY": -0.0699
      },
      {
        "index": 1,
        "AccY": -0.0605
      }],
      "name": "AccY",
    "Calibration": [
      {
        "Bias": "0.0",
        "Gain": "1.0"
      }
    ],
    "MinAttr": -0.0028,
    "MaxAttr": 3.1936
  }
]

Next ! your class should be like this:

public class Points
{
    public int? index { get; set; }
    public int? AccX { get; set; }
    public int? AccY { get; set; }
    public int? AccZ { get; set; }

    public int? gyroZ { get; set; }
}

public class calibration
{
    public string Bias { get; set; }
    public string Gain { get; set; }
}

public class RootObject
{
    public List<Points> points { get; set; }

    public List<calibration> Calibration { get; set; }

    public float? MinAttr { get; set; }
    public float? MaxAttr { get; set; }
}

And, change this line

RootObject[] json = ser.Deserialize<RootObject[]>(ReadJson); // I think it may be array.

Note: your JSON deserialize code is correct.

P.S. I suggest you try Nullable Types instead, or use default properties.

Community
  • 1
  • 1