I am using Newtonsoft.Json and want to deserialize and parse a JSON looking like this, with multiple points in an (array?)
{
"dataFile": {
"point": [
{
"ID": 1,
"time": "17.55",
"m_position": {
"x": 43.311744689941409,
"y": 147.30763244628907,
"z": 25.503124237060548
}
},
{
"ID": 2,
"time": "17.55",
"m_position": {
"x": 43.311744689941409,
"y": 147.30763244628907,
"z": 25.503124237060548
}
},
{
"ID": 3,
"time": "17.55",
"m_position": {
"x": 43.311744689941409,
"y": 147.30763244628907,
"z": 25.503124237060548
}
},
{
"ID": 4,
"time": "17.55",
"m_position": {
"x": 43.311744689941409,
"y": 147.30763244628907,
"z": 25.503124237060548
}
},
{
"ID": 5,
"time": "17.55",
"m_position": {
"x": 43.311744689941409,
"y": 147.30763244628907,
"z": 25.503124237060548
}
},
{
"ID": 6,
"time": "17.55",
"m_position": {
"x": 43.311744689941409,
"y": 147.30763244628907,
"z": 25.503124237060548
}
},
{
"ID": 7,
"time": "17.55",
"m_position": {
"x": 43.311744689941409,
"y": 147.30763244628907,
"z": 25.503124237060548
}
}
]
}
}
I use this to deserialize (the strJSON is the json code i pasted above):
var jPosData = JsonConvert.DeserializeObject<jsonPosSample>(strJSON);
My jsonPosSample class is generated by copying my json and pasting it as a Paste Special>Paste JSON as Classes
So my jsonPosSample class looks like this:
class jsonPosSample
{
public class Rootobject
{
public Datafile dataFile { get; set; }
}
public class Datafile
{
public Point[] point { get; set; }
}
public class Point
{
public int ID { get; set; }
public string time { get; set; }
public M_Position m_position { get; set; }
}
public class M_Position
{
public float x { get; set; }
public float y { get; set; }
public float z { get; set; }
}
}
Edit: Answering comments
Running the json code through the deserilization works:
var jPosData = JsonConvert.DeserializeObject<jsonPosSample>(strJSON);
debugOutput("Here's our JSON object: " + jPosData.ToString());
debugOutput prints out:
Here's our JSON object: Robbat.jsonPosSample
I am struggling to understand how i can store the points inside an array or print them out. I need to make 6 objects and somehow store them into an array, right? How do i do this?
There are two problem sample you have provided.
your json is missing '}' at the end.
JsonConvert.DeserializeObject(strJSON); // As your datafile property is inside this class.
If you want to do for jsonPosSample class then
public class jsonPosSample
{
public Datafile dataFile { get; set; }
}
var result = JsonConvert.DeserializeObject<jsonPosSample>(strJSON);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.