简体   繁体   中英

Invalid JSON primitive: . Trying to deserialize from JSON in C#

I need to deserialize some data serverside from a third party (It is done serverside for accessibility reasons, not by choice). However, I get

I am calling the following line of code:

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

responseFromServer is the following:

{
  "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
   ]
 ]
}

I'm trying to deserialize into an object TPOData:

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

I've tried making the property data to be different. In addition to above I've tried List<object> , List<object[]> , object[][] , object[] , object . I even tried List<List<string>> hoping it might convert the int values. None has helped. I suspect that the issue lies with the data in the 2nd level of the object array is a mixture of int and string values. I have done a very similar task where the 2nd level was entirely strings so I could just use List<List<string>> without problems.

Any assistance would be greatly appreciated.

I tried your example, and it worked fine for me using the JavaScriptSerializer with List<List<object>> inside the TPOData class. The test program I used is copied below. Note I am using the .NET Framework v4.5, in case it matters. Is there something else in the JSON or your code you haven't shown that might be causing the issue?

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

Output:

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

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.

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