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

时间:2018-05-11 11:04:22

标签: c# json

我正在使用Newtonsoft.Json,想要反序列化并解析一个看起来像这样的JSON,在(数组?)中有多个点

The API returned an error: Error: No refresh token is set.

我用它来反序列化(strJSON是我粘贴在上面的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
                }
            }
        ]
    }
}

我的jsonPosSample类是通过复制我的json并将其粘贴为a来生成的 选择性粘贴>将JSON粘贴为类

所以我的jsonPosSample类看起来像这样:

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

编辑:回答评论

通过deserilization运行json代码:

    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; }
    }
}

debugOutput打印出来:

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

我正在努力了解如何将点存储在数组中或打印出来。我需要制作6个对象并以某种方式将它们存储到一个数组中,对吗?我该怎么做?

1 个答案:

答案 0 :(得分:0)

您提供了两个问题示例。

  1. 你的json遗失了&#39;}&#39;在末尾。

  2. JsonConvert.DeserializeObject(strJSON); //由于您的数据文件属性在此类中。

  3. 如果你想为jsonPosSample类做,那么

    public class jsonPosSample
    {
       public Datafile dataFile { get; set; }
    }
    
    var result = JsonConvert.DeserializeObject<jsonPosSample>(strJSON);