简体   繁体   English

反序列化和解析包含多个对象的JSON字符串

[英]Deserialize and parse a JSON string containing multiple objects

I am using Newtonsoft.Json and want to deserialize and parse a JSON looking like this, with multiple points in an (array?) 我正在使用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
                }
            }
        ]
    }
}

I use this to deserialize (the strJSON is the json code i pasted above): 我用它来反序列化(strJSON是我上面粘贴的json代码):

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 我的jsonPosSample类是通过复制json并将其粘贴为Paste Special> Paste JSON as Classes生成的

So my jsonPosSample class looks like this: 所以我的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; }
    }
}

Edit: Answering comments 编辑:回答评论

Running the json code through the deserilization works: 通过反序列化来运行json代码:

      var jPosData = JsonConvert.DeserializeObject<jsonPosSample>(strJSON);
      debugOutput("Here's our JSON object: " + jPosData.ToString());

debugOutput prints out: debugOutput打印出:

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? 我需要制作6个对象,然后以某种方式将它们存储到数组中,对吗? How do i do this? 我该怎么做呢?

There are two problem sample you have provided. 您提供了两个问题样本。

  1. your json is missing '}' at the end. 您的json末尾缺少'}'。

  2. JsonConvert.DeserializeObject(strJSON); JsonConvert.DeserializeObject(strJSON); // As your datafile property is inside this class. //由于您的datafile属性位于此类内。

If you want to do for jsonPosSample class then 如果要对jsonPosSample类进行操作,则

public class jsonPosSample
{
   public Datafile dataFile { get; set; }
}

var result = JsonConvert.DeserializeObject<jsonPosSample>(strJSON);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM