[英]Deserialize and parse a JSON string containing multiple objects
我正在使用Newtonsoft.Json,想反序列化和解析一個JSON像這樣,在(數組?)中有多個點
{
"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
}
}
]
}
}
我用它來反序列化(strJSON是我上面粘貼的json代碼):
var jPosData = JsonConvert.DeserializeObject<jsonPosSample>(strJSON);
我的jsonPosSample類是通過復制json並將其粘貼為Paste Special> Paste JSON as Classes生成的
所以我的jsonPosSample類看起來像這樣:
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; }
}
}
編輯:回答評論
通過反序列化來運行json代碼:
var jPosData = JsonConvert.DeserializeObject<jsonPosSample>(strJSON);
debugOutput("Here's our JSON object: " + jPosData.ToString());
debugOutput打印出:
Here's our JSON object: Robbat.jsonPosSample
我正在努力了解如何將點存儲在數組中或將其打印出來。 我需要制作6個對象,然后以某種方式將它們存儲到數組中,對嗎? 我該怎么做呢?
您提供了兩個問題樣本。
您的json末尾缺少'}'。
JsonConvert.DeserializeObject(strJSON); //由於您的datafile屬性位於此類內。
如果要對jsonPosSample類進行操作,則
public class jsonPosSample
{
public Datafile dataFile { get; set; }
}
var result = JsonConvert.DeserializeObject<jsonPosSample>(strJSON);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.