无效的JSON原语:。试图在C#中从JSON反序列化

时间:2014-07-01 10:19:54

标签: c# json json-deserialization

我需要从第三方反序列化一些数据服务器端(出于可访问性原因,它是在服务器端完成的,而非选择)。但是,我得到了

我正在调用以下代码行:

var data = new JavaScriptSerializer().Deserialize<TPOData>(responseFromServer);

responseFromServer如下:

{
  "name": "TPO",
  "columns": [
    "ogc_fid",
    "orderref",
    "status",
    "entityref",
    "treetype",
    "comments",
    "orderyear",
    "label",
    "dist"
 ],
 "data": [
   [
      360,
      "07/1970/WR ",
      "Tree       ",
      "T6   ",
      "Chestnut",
      "Position checked against Scanned Order 13/11/2008",
      1970,
      "479055.705,204698.514",
      33
   ],
   [
      361,
      "07/1970/WR ",
      "Tree       ",
      "T7   ",
      "May",
      "Position checked against Scanned Order 13/11/2008",
      1970,
      "479061.747,204685.09",
      35
   ]
 ]
}

我正在尝试反序列化为对象TPOData:

public class TPOData
{
    public string name;
    public List<string> columns;
    public List<List<object>> data;
}

我试过让属性数据不同。除此之外,我还尝试了List<object>List<object[]>object[][]object[]object。我甚至试过List<List<string>>希望它可以转换int值。没有人帮忙。我怀疑问题在于对象数组的第二级数据是int和string值的混合。我做了一个非常类似的任务,第二级完全是字符串,所以我可以毫无问题地使用List<List<string>>

非常感谢任何协助。

1 个答案:

答案 0 :(得分:0)

我尝试了您的示例,并且使用JavaScriptSerializerList<List<object>>内的TPOData并使其正常工作。我使用的测试程序复制如下。注意我正在使用.NET Framework v4.5,以防它很重要。 JSON或您的代码中是否还有其他内容可能导致问题?

class Program
{
    static void Main(string[] args)
    {
        string json = @"
        {
            ""name"": ""TPO"",
            ""columns"": [
                ""ogc_fid"",
                ""orderref"",
                ""status"",
                ""entityref"",
                ""treetype"",
                ""comments"",
                ""orderyear"",
                ""label"",
                ""dist""
            ],
            ""data"": [
                [
                    360,
                    ""07/1970/WR "",
                    ""Tree       "",
                    ""T6   "",
                    ""Chestnut"",
                    ""Position checked against Scanned Order 13/11/2008"",
                    1970,
                    ""479055.705,204698.514"",
                    33
                ],
                [
                    361,
                    ""07/1970/WR "",
                    ""Tree       "",
                    ""T7   "",
                    ""May"",
                    ""Position checked against Scanned Order 13/11/2008"",
                    1970,
                    ""479061.747,204685.09"",
                    35
                ]
            ]
        }";

        var data = new JavaScriptSerializer().Deserialize<TPOData>(json);

        Console.WriteLine("name: " + data.name);
        Console.WriteLine();
        foreach (var row in data.data)
        {
            for (int i = 0; i < data.columns.Count; i++)
            {
                Console.WriteLine(data.columns[i] + ": " + row[i]);
            }
            Console.WriteLine();
        }
    }

    public class TPOData
    {
        public string name;
        public List<string> columns;
        public List<List<object>> data;
    }
}

输出:

name: TPO

ogc_fid: 360
orderref: 07/1970/WR
status: Tree
entityref: T6
treetype: Chestnut
comments: Position checked against Scanned Order 13/11/2008
orderyear: 1970
label: 479055.705,204698.514
dist: 33

ogc_fid: 361
orderref: 07/1970/WR
status: Tree
entityref: T7
treetype: May
comments: Position checked against Scanned Order 13/11/2008
orderyear: 1970
label: 479061.747,204685.09
dist: 35