反序列化具有多个对象的Import Io JSON [英] Deserializing Import Io JSON with multiple objects

查看:122
本文介绍了反序列化具有多个对象的Import Io JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好

我正在使用Newtonsoft.JSON从Import Io反序列化JSON响应.我已经成功地使用 http://json2csharp.com/在一个实例中成功构建了数据模型,以获得对数据,但是在这种情况下,仅返回一个JSON对象.我现在正在处理一个新的JSON,该JSON将与多个对象一起返回,并且遇到以下错误消息:

Im using Newtonsoft.JSON to deserialize a JSON response from Import Io. I have successfully used http://json2csharp.com/ to build a data model successfully in one instance to gain access to the data, however in that instance only one JSON object was returned. I am now working on a new piece of JSON that is being returned with multiple objects and i'm hitting issues with the following error message:

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."

Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[OSP.Shop_DTO+RootObject]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'url', line 1, position 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": "$36.00"
                                }
                            ],
                            "BN_shop_2KG": [
                                {
                                    "text": "$69"
                                }
                            ],
                            "BN_shop_3KG": [
                                {
                                    "text": "$97"
                                }
                            ],
                            "BN_VEGE_5KG": [
                                {
                                    "text": "3 KG = $97"
                                }
                            ],
                            "BN_shop_4KG": [
                                {
                                    "text": "$124"
                                }
                            ],
                            "BN_shop_5KG": [
                                {
                                    "text": "$149"
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        "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": "$27.00"
                                }
                            ],
                            "BN_shop_2KG": [
                                {
                                    "text": "$49"
                                }
                            ],
                            "BN_shop_3KG": [
                                {
                                    "text": "$69"
                                }
                            ],
                            "BN_VEGE_5KG": [
                                {
                                    "text": "3 KG = $69"
                                }
                            ],
                            "BN_shop_4KG": [
                                {
                                    "text": "$84"
                                }
                            ],
                            "BN_shop_5KG": [
                                {
                                    "text": "$99"
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        "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": "$29.00"
                                }
                            ],
                            "BN_shop_2KG": [
                                {
                                    "text": "$56"
                                }
                            ],
                            "BN_shop_3KG": [
                                {
                                    "text": "$79"
                                }
                            ],
                            "BN_VEGE_5KG": [
                                {
                                    "text": "3 KG = $79"
                                }
                            ],
                            "BN_shop_4KG": [
                                {
                                    "text": "$99"
                                }
                            ],
                            "BN_shop_5KG": [
                                {
                                    "text": "$119"
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        "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": "$25.00"
                                }
                            ],
                            "BN_shop_2KG": [
                                {
                                    "text": "$45"
                                }
                            ],
                            "BN_shop_3KG": [
                                {
                                    "text": "$89"
                                }
                            ],
                            "BN_VEGE_5KG": [
                                {
                                    "text": "5 KG = $89"
                                }
                            ],
                            "BN_shop_4KG": [
                                {
                                    "text": "$175"
                                }
                            ],
                            "BN_shop_5KG": [
                                {
                                    "text": "$339"
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        "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": "$25.00"
                                }
                            ],
                            "BN_shop_2KG": [
                                {
                                    "text": "$45"
                                }
                            ],
                            "BN_shop_3KG": [
                                {
                                    "text": "$89"
                                }
                            ],
                            "BN_VEGE_5KG": [
                                {
                                    "text": "5 KG = $89"
                                }
                            ],
                            "BN_shop_4KG": [
                                {
                                    "text": "$169"
                                }
                            ],
                            "BN_shop_5KG": [
                                {
                                    "text": "$319"
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        "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"中的文本"值.此项:

In my code i'm trying to get access to the 'text' value in 'BN_shop_1KG' from the first JSON object. This item:

"BN_shop_1KG": [
   {
        "text": "$36.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);

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

Any help would be hugely appreciated, i've been stuck on this for a couple of days :(

推荐答案

Json文件无效.由于它包含多个Json对象,因此它应该是一个Json数组,需要用[]进行包装,并且在每个Json对象之间还有另一件事,您需要,"

The Json file is invalid. Since it contains multiple Json object, it should be a Json array which needs to be wrapped by [], and another thing is between each Json object, you need ","

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"": ""$36.00""
                            }
                        ],
                        ""BN_shop_2KG"": [
                            {
                                ""text"": ""$69""
                            }
                        ],
                        ""BN_shop_3KG"": [
                            {
                                ""text"": ""$97""
                            }
                        ],
                        ""BN_VEGE_5KG"": [
                            {
                                ""text"": ""3 KG = $97""
                            }
                        ],
                        ""BN_shop_4KG"": [
                            {
                                ""text"": ""$124""
                            }
                        ],
                        ""BN_shop_5KG"": [
                            {
                                ""text"": ""$149""
                            }
                        ]
                    }
                ]
            }
        ]
    },
    ""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"": ""$27.00""
                            }
                        ],
                        ""BN_shop_2KG"": [
                            {
                                ""text"": ""$49""
                            }
                        ],
                        ""BN_shop_3KG"": [
                            {
                                ""text"": ""$69""
                            }
                        ],
                        ""BN_VEGE_5KG"": [
                            {
                                ""text"": ""3 KG = $69""
                            }
                        ],
                        ""BN_shop_4KG"": [
                            {
                                ""text"": ""$84""
                            }
                        ],
                        ""BN_shop_5KG"": [
                            {
                                ""text"": ""$99""
                            }
                        ]
                    }
                ]
            }
        ]
    },
    ""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"": ""$29.00""
                            }
                        ],
                        ""BN_shop_2KG"": [
                            {
                                ""text"": ""$56""
                            }
                        ],
                        ""BN_shop_3KG"": [
                            {
                                ""text"": ""$79""
                            }
                        ],
                        ""BN_VEGE_5KG"": [
                            {
                                ""text"": ""3 KG = $79""
                            }
                        ],
                        ""BN_shop_4KG"": [
                            {
                                ""text"": ""$99""
                            }
                        ],
                        ""BN_shop_5KG"": [
                            {
                                ""text"": ""$119""
                            }
                        ]
                    }
                ]
            }
        ]
    },
    ""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"": ""$25.00""
                            }
                        ],
                        ""BN_shop_2KG"": [
                            {
                                ""text"": ""$45""
                            }
                        ],
                        ""BN_shop_3KG"": [
                            {
                                ""text"": ""$89""
                            }
                        ],
                        ""BN_VEGE_5KG"": [
                            {
                                ""text"": ""5 KG = $89""
                            }
                        ],
                        ""BN_shop_4KG"": [
                            {
                                ""text"": ""$175""
                            }
                        ],
                        ""BN_shop_5KG"": [
                            {
                                ""text"": ""$339""
                            }
                        ]
                    }
                ]
            }
        ]
    },
    ""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"": ""$25.00""
                            }
                        ],
                        ""BN_shop_2KG"": [
                            {
                                ""text"": ""$45""
                            }
                        ],
                        ""BN_shop_3KG"": [
                            {
                                ""text"": ""$89""
                            }
                        ],
                        ""BN_VEGE_5KG"": [
                            {
                                ""text"": ""5 KG = $89""
                            }
                        ],
                        ""BN_shop_4KG"": [
                            {
                                ""text"": ""$169""
                            }
                        ],
                        ""BN_shop_5KG"": [
                            {
                                ""text"": ""$319""
                            }
                        ]
                    }
                ]
            }
        ]
    },
    ""pageData"": {
        ""resourceId"": ""f91e05d0265ab5a5f7f948c57a05bced"",
        ""statusCode"": 200,
        ""timestamp"": 1476432605227
    },
    ""timestamp"": 1476432606451,
    ""sequenceNumber"": 4
}
}]"

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

Then you can use your code to get the price. Btw, there is a typo in your code, BN_shop_1kg is supposed to be BN_shop_1KG

这篇关于反序列化具有多个对象的Import Io JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆