簡體   English   中英

為什么我不能使用JSON.NET反序列化此JSON?

[英]Why can't I deserialize this JSON with JSON.NET?

背景

因此,我決定對某些C#程序開始使用JSON而不是XML。 自然,我發現的第一件事是Newtonsoft的JSON.NET。 在.NET世界中進行一些JSON解析似乎是一件合理的事情,並且每個人都在Microsoft提供的內置序列化程序中提出了建議。 這就是我決定使用的方式。 到目前為止,除了諸如此類的非常瑣碎的示例之外,我無法反序列化任何內容

我正在嘗試將以下JSON反序列化為有意義的對象,以供我的程序使用,但是每次嘗試這樣做時,應用程序就會崩潰。 我對異常消息中發生的事情有一個非常模糊的解釋。 我沒有電話號碼或其他任何信息,只是說...

附加信息:無法將當前JSON數組(例如[1,2,3])反序列化為類型'JsonTest.testObjects',因為該類型需要JSON對象(例如{“ name”:“ value”})才能正確反序列化。

要解決此錯誤,可以將JSON更改為JSON對象(例如{“ name”:“ value”}),也可以將反序列化類型更改為數組,或者將實現集合接口的類型(例如ICollection,IList)更改為List,例如List從JSON數組反序列化。 還可以將JsonArrayAttribute添加到類型中,以強制其從JSON數組反序列化。

也許是說我的第一個對象有問題? 在進入子類之前,我不會在這里使用任何數組,我不確定在這里發生了什么。

{
    "name":"myTest",
    "testObjects":[
        "testObject":
            "name":"Operator Tests",
            "index":1,
            "description":"Test out the operator interface.",
            "SubTests":[
                "SubTest":
                    "name":"Display Test",
                    "index":4,
                    "description":"Testing for display faults.",
                    "Steps":[
                        "Step":
                            "name":"Configuration #1",
                            "Parameters":[
                                "Mode-A",
                                "Unit #1"
                            ],
                        "Step":
                            "name":"Configuration #2",
                            "Parameters":[
                                "Mode-B",
                                "Unit #1"
                            ],
                        "Step":
                            "name":"Configuration #3",
                            "Parameters":[
                                "Mode-C",
                                "Unit #1"
                            ]
                    ]
            ]
    ]
}

好,然后是反序列化代碼。

    Test newTest = null;
    String testData = String.Empty;

    // read the file
    testData = File.ReadAllText("Test.json");

    // de-serialize the JSON
    newTest = JsonConvert.DeserializeObject<Test>(testData);

然后,這是要反序列化的Test類。 Test對象是容納所有testObjects的對象。 除非明確要求,否則最初我不會提供有關子類的信息,因為我認為問題可能出在我的JSON開頭。 只要有人問我,我會提供盡可能多的細節!

為什么不進行序列化,該異常/消息實際上告訴我什么?

不合法的JSON:使用http://jsonlint.com/

{
    "name": "myTest",
    "testObjects": [
        "testObject":
        "name": "Operator Tests",
        "index": 1,

testObjects是一個數組,但是您可以在其中指定鍵值對。 因此,內容應該是一個對象或對象列表。

我想你想要這樣的東西:

{
    "name":"myTest",
    "testObjects":[
        {
            "name":"Operator Tests",
            "index":1,
            "description":"Test out the operator interface.",
            "SubTests":[
                {
                    "name":"Display Test",
                    "index":4,
                    "description":"Testing for display faults.",
                    "Steps":[
                        {
                            "name":"Configuration #1",
                            "Parameters":[
                                "Mode-A",
                                "Unit #1"
                            ]
                        },
                        {
                            "name":"Configuration #2",
                            "Parameters":[
                                "Mode-B",
                                "Unit #1"
                            ]
                        },
                        {
                            "name":"Configuration #3",
                            "Parameters":[
                                "Mode-C",
                                "Unit #1"
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

要從JSON生成C#類,請嘗試使用: http : //json2csharp.com/

有效的 JSON生成:

public class Step
{
    public string name { get; set; }
    public List<string> Parameters { get; set; }
}

public class SubTest
{
    public string name { get; set; }
    public int index { get; set; }
    public string description { get; set; }
    public List<Step> Steps { get; set; }
}

public class TestObject
{
    public string name { get; set; }
    public int index { get; set; }
    public string description { get; set; }
    public List<SubTest> SubTests { get; set; }
}

public class RootObject
{
    public string name { get; set; }
    public List<TestObject> testObjects { get; set; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM