使用多个对象反序列化 Import Io JSON

Deserializing Import Io JSON with multiple objects

大家早上好,

我正在使用 Newtonsoft.JSON 反序列化来自 Import Io 的 JSON 响应。我已经成功地使用 http://json2csharp.com/ 在一个实例中成功地构建了一个数据模型以访问数据,但是在那个实例中只返回了一个 JSON 对象。我现在正在处理一个新的 JSON 片段,它与多个对象一起返回,我遇到了以下错误消息的问题:

Newtonsoft.Json.JsonSerializationException:无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List`1[OSP.Shop_DTO+RootObject]',因为该类型需要 JSON 数组(例如 [1,2,3])以正确反序列化。 要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或更改反序列化类型,使其成为普通的 .NET 类型(例如,不是原始类型像整数,而不是像数组或列表这样的集合类型),可以从 JSON 对象反序列化。 JsonObjectAttribute 也可以添加到类型以强制它从 JSON 对象反序列化。 路径 'url',第 1 行,位置 7。"

我的 JSON 看起来像这样:

{
    "url": "http://www.myurl.com/-shop-item.html",
    "result": {
        "extractorData": {
            "url": "http://www.myurl.com/-shop-item.html",
            "resourceId": "1db49f66afc2f234cb5ab470f0c39e0c",
                "data": [
                {
                    "group": [
                        {
                            "BN_shop_1KG": [
                                {
                                    "text": ".00"
                                }
                            ],
                            "BN_shop_2KG": [
                                {
                                    "text": ""
                                }
                            ],
                            "BN_shop_3KG": [
                                {
                                    "text": ""
                                }
                            ],
                            "BN_VEGE_5KG": [
                                {
                                    "text": "3 KG = "
                                }
                            ],
                            "BN_shop_4KG": [
                                {
                                    "text": "4"
                                }
                            ],
                            "BN_shop_5KG": [
                                {
                                    "text": "9"
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        "pageData": {
            "resourceId": "1db49f66afc2f234cb5ab470f0c39e0c",
            "statusCode": 200,
            "timestamp": 1476432605275
        },
        "timestamp": 1476432606594,
        "sequenceNumber": 0
    }
}{
    "url": "http://www.myurl.com/-shop-concentrate.html",
    "result": {
        "extractorData": {
            "url": "http://www.myurl.com/-shop-concentrate.html",
            "resourceId": "dd4837cc7d0085eae005243c7bd8ca8a",
            "data": [
                {
                    "group": [
                        {
                            "BN_shop_1KG": [
                                {
                                    "text": ".00"
                                }
                            ],
                            "BN_shop_2KG": [
                                {
                                    "text": ""
                                }
                            ],
                            "BN_shop_3KG": [
                                {
                                    "text": ""
                                }
                            ],
                            "BN_VEGE_5KG": [
                                {
                                    "text": "3 KG = "
                                }
                            ],
                            "BN_shop_4KG": [
                                {
                                    "text": ""
                                }
                            ],
                            "BN_shop_5KG": [
                                {
                                    "text": ""
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        "pageData": {
            "resourceId": "dd4837cc7d0085eae005243c7bd8ca8a",
            "statusCode": 200,
            "timestamp": 1476432604237
        },
        "timestamp": 1476432605124,
        "sequenceNumber": 1
    }
}{
    "url": "http://www.myurl.comshop-matrix.html",
    "result": {
        "extractorData": {
            "url": "http://www.myurl.comshop-matrix.html",
            "resourceId": "deee30ffa0098e017a06c0e0c805e133",
            "data": [
                {
                    "group": [
                        {
                            "BN_shop_1KG": [
                                {
                                    "text": ".00"
                                }
                            ],
                            "BN_shop_2KG": [
                                {
                                    "text": ""
                                }
                            ],
                            "BN_shop_3KG": [
                                {
                                    "text": ""
                                }
                            ],
                            "BN_VEGE_5KG": [
                                {
                                    "text": "3 KG = "
                                }
                            ],
                            "BN_shop_4KG": [
                                {
                                    "text": ""
                                }
                            ],
                            "BN_shop_5KG": [
                                {
                                    "text": "9"
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        "pageData": {
            "resourceId": "deee30ffa0098e017a06c0e0c805e133",
            "statusCode": 200,
            "timestamp": 1476432602408
        },
        "timestamp": 1476432603204,
        "sequenceNumber": 2
    }
}{
    "url": "http://www.myurl.comsoy-shop-item.html",
    "result": {
        "extractorData": {
            "url": "http://www.myurl.comsoy-shop-item.html",
            "resourceId": "5593aad40f95ba868626e47a1b550813",
            "data": [
                {
                    "group": [
                        {
                            "BN_shop_1KG": [
                                {
                                    "text": ".00"
                                }
                            ],
                            "BN_shop_2KG": [
                                {
                                    "text": ""
                                }
                            ],
                            "BN_shop_3KG": [
                                {
                                    "text": ""
                                }
                            ],
                            "BN_VEGE_5KG": [
                                {
                                    "text": "5 KG = "
                                }
                            ],
                            "BN_shop_4KG": [
                                {
                                    "text": "5"
                                }
                            ],
                            "BN_shop_5KG": [
                                {
                                    "text": "9"
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        "pageData": {
            "resourceId": "5593aad40f95ba868626e47a1b550813",
            "statusCode": 200,
            "timestamp": 1476432602479
        },
        "timestamp": 1476432603847,
        "sequenceNumber": 3
    }
}{
    "url": "http://www.myurl.compea-shop.html",
    "result": {
        "extractorData": {
            "url": "http://www.myurl.compea-shop.html",
            "resourceId": "f91e05d0265ab5a5f7f948c57a05bced",
            "data": [
                {
                    "group": [
                        {
                            "BN_shop_1KG": [
                                {
                                    "text": ".00"
                                }
                            ],
                            "BN_shop_2KG": [
                                {
                                    "text": ""
                                }
                            ],
                            "BN_shop_3KG": [
                                {
                                    "text": ""
                                }
                            ],
                            "BN_VEGE_5KG": [
                                {
                                    "text": "5 KG = "
                                }
                            ],
                            "BN_shop_4KG": [
                                {
                                    "text": "9"
                                }
                            ],
                            "BN_shop_5KG": [
                                {
                                    "text": "9"
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        "pageData": {
            "resourceId": "f91e05d0265ab5a5f7f948c57a05bced",
            "statusCode": 200,
            "timestamp": 1476432605227
        },
        "timestamp": 1476432606451,
        "sequenceNumber": 4
    }
}

模型如下所示:

public class BNShop1KG
{
    public string text { get; set; }
}

public class BNShop2KG
{
    public string text { get; set; }
}

public class BNShop3KG
{
    public string text { get; set; }
}

public class BNVEGE5KG
{
    public string text { get; set; }
}

public class BNShop4KG
{
    public string text { get; set; }
}

public class BNShop5KG
{
    public string text { get; set; }
}

public class Group
{
    public List<BNShop1KG> BN_shop_1KG { get; set; }
    public List<BNShop2KG> BN_shop_2KG { get; set; }
    public List<BNShop3KG> BN_shop_3KG { get; set; }
    public List<BNVEGE5KG> BN_VEGE_5KG { get; set; }
    public List<BNShop4KG> BN_shop_4KG { get; set; }
    public List<BNShop5KG> BN_shop_5KG { get; set; }
}

public class Datum
{
    public List<Group> group { get; set; }
}

public class ExtractorData
{
    public string url { get; set; }
    public string resourceId { get; set; }
    public List<Datum> data { get; set; }
}

public class PageData
{
    public string resourceId { get; set; }
    public int statusCode { get; set; }
    public long timestamp { get; set; }
}

public class Result
{
    public ExtractorData extractorData { get; set; }
    public PageData pageData { get; set; }
    public long timestamp { get; set; }
    public int sequenceNumber { get; set; }
}

public class RootObject
{
    public string url { get; set; }
    public Result result { get; set; }
}

在我的代码中,我试图从第一个 JSON 对象访问 'BN_shop_1KG' 中的 'text' 值。此项:

"BN_shop_1KG": [
   {
        "text": ".00"
   }

我已经在我的代码中尝试了以下操作,但收到了上述错误。据我了解,阅读一些以前的线程是我需要使用反序列化到 List 来处理以这种方式收到的 JSON:

    List<Shop_DTO.RootObject> obj = JsonConvert.DeserializeObject<List<Shop_DTO.RootObject>>(_rawHtmlResult);

    var price = obj.First().result.extractorData.data[0].group[0].BN_shop_1kg[0].text;
    Debug.WriteLine("Price for item {0}", price);

任何帮助将不胜感激,我已经坚持了几天:(

Json 文件无效。由于里面包含多个Json对象,所以应该是一个Json数组,需要用[]包裹起来,还有就是每个Json对象之间,需要","

var jsonText = @"[{
""url"": ""http://www.myurl.com/-shop-item.html"",
""result"": {
    ""extractorData"": {
        ""url"": ""http://www.myurl.com/-shop-item.html"",
        ""resourceId"": ""1db49f66afc2f234cb5ab470f0c39e0c"",
            ""data"": [
            {
                ""group"": [
                    {
                        ""BN_shop_1KG"": [
                            {
                                ""text"": "".00""
                            }
                        ],
                        ""BN_shop_2KG"": [
                            {
                                ""text"": """"
                            }
                        ],
                        ""BN_shop_3KG"": [
                            {
                                ""text"": """"
                            }
                        ],
                        ""BN_VEGE_5KG"": [
                            {
                                ""text"": ""3 KG = ""
                            }
                        ],
                        ""BN_shop_4KG"": [
                            {
                                ""text"": ""4""
                            }
                        ],
                        ""BN_shop_5KG"": [
                            {
                                ""text"": ""9""
                            }
                        ]
                    }
                ]
            }
        ]
    },
    ""pageData"": {
        ""resourceId"": ""1db49f66afc2f234cb5ab470f0c39e0c"",
        ""statusCode"": 200,
        ""timestamp"": 1476432605275
    },
    ""timestamp"": 1476432606594,
    ""sequenceNumber"": 0
}
},{
""url"": ""http://www.myurl.com/-shop-concentrate.html"",
""result"": {
    ""extractorData"": {
        ""url"": ""http://www.myurl.com/-shop-concentrate.html"",
        ""resourceId"": ""dd4837cc7d0085eae005243c7bd8ca8a"",
        ""data"": [
            {
                ""group"": [
                    {
                        ""BN_shop_1KG"": [
                            {
                                ""text"": "".00""
                            }
                        ],
                        ""BN_shop_2KG"": [
                            {
                                ""text"": """"
                            }
                        ],
                        ""BN_shop_3KG"": [
                            {
                                ""text"": """"
                            }
                        ],
                        ""BN_VEGE_5KG"": [
                            {
                                ""text"": ""3 KG = ""
                            }
                        ],
                        ""BN_shop_4KG"": [
                            {
                                ""text"": """"
                            }
                        ],
                        ""BN_shop_5KG"": [
                            {
                                ""text"": """"
                            }
                        ]
                    }
                ]
            }
        ]
    },
    ""pageData"": {
        ""resourceId"": ""dd4837cc7d0085eae005243c7bd8ca8a"",
        ""statusCode"": 200,
        ""timestamp"": 1476432604237
    },
    ""timestamp"": 1476432605124,
    ""sequenceNumber"": 1
}
},{
""url"": ""http://www.myurl.comshop-matrix.html"",
""result"": {
    ""extractorData"": {
        ""url"": ""http://www.myurl.comshop-matrix.html"",
        ""resourceId"": ""deee30ffa0098e017a06c0e0c805e133"",
        ""data"": [
            {
                ""group"": [
                    {
                        ""BN_shop_1KG"": [
                            {
                                ""text"": "".00""
                            }
                        ],
                        ""BN_shop_2KG"": [
                            {
                                ""text"": """"
                            }
                        ],
                        ""BN_shop_3KG"": [
                            {
                                ""text"": """"
                            }
                        ],
                        ""BN_VEGE_5KG"": [
                            {
                                ""text"": ""3 KG = ""
                            }
                        ],
                        ""BN_shop_4KG"": [
                            {
                                ""text"": """"
                            }
                        ],
                        ""BN_shop_5KG"": [
                            {
                                ""text"": ""9""
                            }
                        ]
                    }
                ]
            }
        ]
    },
    ""pageData"": {
        ""resourceId"": ""deee30ffa0098e017a06c0e0c805e133"",
        ""statusCode"": 200,
        ""timestamp"": 1476432602408
    },
    ""timestamp"": 1476432603204,
    ""sequenceNumber"": 2
}
},{
""url"": ""http://www.myurl.comsoy-shop-item.html"",
""result"": {
    ""extractorData"": {
        ""url"": ""http://www.myurl.comsoy-shop-item.html"",
        ""resourceId"": ""5593aad40f95ba868626e47a1b550813"",
        ""data"": [
            {
                ""group"": [
                    {
                        ""BN_shop_1KG"": [
                            {
                                ""text"": "".00""
                            }
                        ],
                        ""BN_shop_2KG"": [
                            {
                                ""text"": """"
                            }
                        ],
                        ""BN_shop_3KG"": [
                            {
                                ""text"": """"
                            }
                        ],
                        ""BN_VEGE_5KG"": [
                            {
                                ""text"": ""5 KG = ""
                            }
                        ],
                        ""BN_shop_4KG"": [
                            {
                                ""text"": ""5""
                            }
                        ],
                        ""BN_shop_5KG"": [
                            {
                                ""text"": ""9""
                            }
                        ]
                    }
                ]
            }
        ]
    },
    ""pageData"": {
        ""resourceId"": ""5593aad40f95ba868626e47a1b550813"",
        ""statusCode"": 200,
        ""timestamp"": 1476432602479
    },
    ""timestamp"": 1476432603847,
    ""sequenceNumber"": 3
}
},{
""url"": ""http://www.myurl.compea-shop.html"",
""result"": {
    ""extractorData"": {
        ""url"": ""http://www.myurl.compea-shop.html"",
        ""resourceId"": ""f91e05d0265ab5a5f7f948c57a05bced"",
        ""data"": [
            {
                ""group"": [
                    {
                        ""BN_shop_1KG"": [
                            {
                                ""text"": "".00""
                            }
                        ],
                        ""BN_shop_2KG"": [
                            {
                                ""text"": """"
                            }
                        ],
                        ""BN_shop_3KG"": [
                            {
                                ""text"": """"
                            }
                        ],
                        ""BN_VEGE_5KG"": [
                            {
                                ""text"": ""5 KG = ""
                            }
                        ],
                        ""BN_shop_4KG"": [
                            {
                                ""text"": ""9""
                            }
                        ],
                        ""BN_shop_5KG"": [
                            {
                                ""text"": ""9""
                            }
                        ]
                    }
                ]
            }
        ]
    },
    ""pageData"": {
        ""resourceId"": ""f91e05d0265ab5a5f7f948c57a05bced"",
        ""statusCode"": 200,
        ""timestamp"": 1476432605227
    },
    ""timestamp"": 1476432606451,
    ""sequenceNumber"": 4
}
}]"

然后您可以使用您的代码获取价格。顺便说一句,您的代码中有错字,BN_shop_1kg 应该是 BN_shop_1KG