首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从XML转换后使用Json.Net查询JSON时遇到问题。

从XML转换后使用Json.Net查询JSON时遇到问题。
EN

Stack Overflow用户
提问于 2019-08-08 20:28:49
回答 1查看 79关注 0票数 1

我有一些XML,我通过调用此方法将其转换为JSON:

代码语言:javascript
复制
public static string SerializeAsJson(string xml)
{
    if (string.IsNullOrEmpty(xml)) throw new ArgumentNullException(nameof(xml));
    if (xml.StartsWith("{") && xml.EndsWith("}")) return xml;

    var doc = new XmlDocument();
    doc.LoadXml(xml);

    var json = JsonConvert.SerializeXmlNode(doc);
    return json;
}

这将返回以下内容:

代码语言:javascript
复制
{
"?xml":{
    "@version":"1.0",
    "@encoding":"UTF-8"
},
"product":{
    "productname":{
        "#cdata-section":"Artisan Cook Processor - Onyx Black, 4.5 litres, Keep-warm function, Dishwasher-safe pot, Timer"
    },
    "productshortname":{
        "#cdata-section":"Artisan Cook Processor - Onyx Black"
    },
    "productshortdesc":{
        "#cdata-section":"Rethink the way you cook with the KitchenAid Artisan Cook Processor in stylish onyx black. The ultimate helping hand Create delicious meals in no time with this all-in-one, multifunctional food processor. It really can do everything you need to cook up a feast - chop, crush, blend, mix, steam, steam and fry to name a few of the 25 helpful functions. Additionally, this KitchenAid worktop cooker benefits from a clever StirAssist asset, which gently flips and stirs your ingredients, so they still have the �stirred-by-hand', home-cooked effect when combined together. Simplicity, control and precision Create elegantly simple meals for one or test yourself with a whole dinner party of treats from scratch with the Artisan Cook Processor . The KitchenAid really makes life in the kitchen easier � use the hinged lid to open your processor whilst it is working, to add ingredients as you go, reducing mess and saving you time. Uniquely stylish The 4.5 litre KitchenAid Artisan comes in stainless steel with the added benefit of ergonomic, comfortable handles and a hinged lid for easy opening. It is reliable, robust and easy to clean with dishwasher-safe parts, meaning you don't need to spend hours manually maintaining it. Premium details such as metal knobs, levels and a die-cast metal body make this KitchenAid a real focal point of the kitchen, particularly with its striking onyx black colouring. Included extras Along with your KitchenAid Artisan Cook Processor , enjoy the step-by-step and easy to follow recipes for cooking delicious meals in your KitchenAid Artisan Processor . If you are looking for a streamlined appliance that has multiple functions covered, consider the KitchenAid Artisan Cook Processor."
    },
    "producthtmldesc":{
        "#cdata-section":"<p>Rethink the way you cook with the KitchenAid <strong>Artisan Cook Processor</strong> in stylish onyx black.<br /> <br /> <strong>The ultimate helping hand</strong><br /> <br /> Create delicious meals in no time with this all-in-one, multifunctional food processor. It really can do everything you need to cook up a feast - chop, crush, blend, mix, steam, steam and fry to name a few of the 25 helpful functions.<br /> <br /> Additionally, this KitchenAid worktop cooker benefits from a clever StirAssist asset, which gently flips and stirs your ingredients, so they still have the �stirred-by-hand', home-cooked effect when combined together.<br /> <br /> <strong>Simplicity, control and precision</strong><br /> <br /> Create elegantly simple meals for one or test yourself with a whole dinner party of treats from scratch with the <strong>Artisan Cook Processor</strong>. The KitchenAid really makes life in the kitchen easier � use the hinged lid to open your processor whilst it is working, to add ingredients as you go, reducing mess and saving you time.<br /> <br /> <strong>Uniquely stylish</strong><br /> <br /> The 4.5 litre KitchenAid <strong>Artisan</strong> comes in stainless steel with the added benefit of ergonomic, comfortable handles and a hinged lid for easy opening. It is reliable, robust and easy to clean with dishwasher-safe parts, meaning you dont need to spend hours manually maintaining it.<br /> <br /> Premium details such as metal knobs, levels and a die-cast metal body make this KitchenAid a real focal point of the kitchen, particularly with its striking onyx black colouring.<br /> <br /> <strong>Included extras</strong><br /> <br /> Along with your KitchenAid <strong>Artisan Cook Processor</strong>, enjoy the step-by-step and easy to follow recipes for cooking delicious meals in your KitchenAid <strong>Artisan Processor</strong>.<br /> <br /> If you are looking for a streamlined appliance that has multiple functions covered, consider the KitchenAid <strong>Artisan Cook Processor. </strong></p>"
    },
    "productlongdesc":{
        "#cdata-section":"Rethink the way you cook with the KitchenAid Artisan Cook Processor in stylish onyx black. The ultimate helping hand Create delicious meals in no time with this all-in-one, multifunctional food processor. It really can do everything you need to cook up a feast - chop, crush, blend, mix, steam, steam and fry to name a few of the 25 helpful functions. Additionally, this KitchenAid worktop cooker benefits from a clever StirAssist asset, which gently flips and stirs your ingredients, so they still have the �stirred-by-hand', home-cooked effect when combined together. Simplicity, control and precision Create elegantly simple meals for one or test yourself with a whole dinner party of treats from scratch with the Artisan Cook Processor . The KitchenAid really makes life in the kitchen easier � use the hinged lid to open your processor whilst it is working, to add ingredients as you go, reducing mess and saving you time. Uniquely stylish The 4.5 litre KitchenAid Artisan comes in stainless steel with the added benefit of ergonomic, comfortable handles and a hinged lid for easy opening. It is reliable, robust and easy to clean with dishwasher-safe parts, meaning you don't need to spend hours manually maintaining it. Premium details such as metal knobs, levels and a die-cast metal body make this KitchenAid a real focal point of the kitchen, particularly with its striking onyx black colouring. Included extras Along with your KitchenAid Artisan Cook Processor , enjoy the step-by-step and easy to follow recipes for cooking delicious meals in your KitchenAid Artisan Processor . If you are looking for a streamlined appliance that has multiple functions covered, consider the KitchenAid Artisan Cook Processor."
    },
    "productsecondaryname":{
        "#cdata-section":"4.5 litres, Keep-warm function, Dishwasher-safe pot, Timer"
    },
    "brand":{
        "#cdata-section":"KITCHENAID"
    },
    "infostech":null,
    "refexternal":{
        "#cdata-section":"144626"
    },
    "infotech":{
        "#cdata-section":"[{\\"         Title\\":\\"OVERVIEW\\",
        \\"Priority\\":1,
        \\"Attributes\\":[
            {
            \\"Title\\":\\"Type\\",
            \\"Value\\":\\"Multicooker\\",
            \\"Priority\\":0
            },
            {
            \\"Title\\":\\"Power\\",
            \\"Value\\":\\"1500 W\\",
            \\"Priority\\":2
            },
            {
            \\"Title\\":\\"Capacity\\",
            \\"Value\\":\\"4.5 litres\\",
            \\"Priority\\":1
            }
        ]
    },
    {
        \\"Title\\":\\"FUNCTIONS\\",
        \\"Priority\\":2,
        \\"Attributes\\":[
            {
            \\"Title\\":\\"Number of programmes\\",
            \\"Value\\":\\"25\\",
            \\"Priority\\":4
            },
            {
            \\"Title\\":\\"Timer\\",
            \\"Value\\":\\"Yes\\",
            \\"Priority\\":8
            },
            {
            \\"Title\\":\\"Number of heat settings\\",
            \\"Value\\":\\"Up to 140�C\\",
            \\"Priority\\":7
            },
            {
            \\"Title\\":\\"Keep warm function\\",
            \\"Value\\":\\"Yes\\",
            \\"Priority\\":6
            },
            {
            \\"Title\\":\\"Programs\\",
            \\"Value\\":\\"- Boil- Fry- Stew- Steam- Puree- Dough\\",
            \\"Priority\\":5
            }
        ]
    },
    {
        \\"Title\\":\\"FEATURES\\",
        \\"Priority\\":3,
        \\"Attributes\\":[
            {
            \\"Title\\":\\"Digital display\\",
            \\"Value\\":\\"Yes\\",
            \\"Priority\\":10
            },
            {
            \\"Title\\":\\"Automatic shut-off\\",
            \\"Value\\":\\"Yes\\",
            \\"Priority\\":18
            },
            {
            \\"Title\\":\\"Dishwasher safe parts\\",
            \\"Value\\":\\"Yes\\",
            \\"Priority\\":17
            },
            {
            \\"Title\\":\\"Microwave safe pot\\",
            \\"Value\\":\\"No\\",
            \\"Priority\\":16
            },
            {
            \\"Title\\":\\"Oven safe pot\\",
            \\"Value\\":\\"No\\",
            \\"Priority\\":15
            },
            {
            \\"Title\\":\\"Number of tiers\\",
            \\"Value\\":\\"3\\",
            \\"Priority\\":14
            },
            {
            \\"Title\\":\\"Sound signal\\",
            \\"Value\\":\\"Yes\\",
            \\"Priority\\":12
            },
            {
            \\"Title\\":\\"Power indicator\\",
            \\"Value\\":\\"Yes\\",
            \\"Priority\\":11
            }
        ]
    },
    {
        \\"Title\\":\\"GENERAL\\",
        \\"Priority\\":4,
        \\"Attributes\\":[
            {
            \\"Title\\":\\"Colour\\",
            \\"Value\\":\\"Black\\",
            \\"Priority\\":20
            },
            {
            \\"Title\\":\\"Manufacturer's guarantee\\",
            \\"Value\\":\\"3 years\\",
            \\"Priority\\":26
            },
            {
            \\"Title\\":\\"Weight\\",
            \\"Value\\":\\"10.14 kg\\",
            \\"Priority\\":25
            },
            {
            \\"Title\\":\\"Dimensions \\",
            \\"Value\\":\\"411 x 314 x 342 mm (H x W x D)\\",
            \\"Priority\\":24
            },
            {
            \\"Title\\":\\"Power cord length\\",
            \\"Value\\":\\"1 m\\",
            \\"Priority\\":23
            },
            {
            \\"Title\\":\\"Power cord storage\\",
            \\"Value\\":\\"No\\",
            \\"Priority\\":22
            },
            {
            \\"Title\\":\\"Box contents\\",
            \\"Value\\":\\"- StirAssist- Egg whip- Dough blade- Multiblade- Upper and lower steam baskets with lid- Inner basket- Mini bowl and mini multiblade- Measuring cup- Free cookbook\\",
            \\"Priority\\":21
            }
        ]
    }
]   "},"   id":{
    "#cdata-section":"10136069"
},
"images":{
    "urlimage1":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/l_10136069.jpg",
    "urlimage2":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/g_10136069.jpg",
    "urlimage3":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/m_10136069.jpg",
    "urlimage4":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/s_10136069.jpg",
    "urlimage5":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/t_10136069.jpg",
    "urlimage6":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/u_10136069.jpg"
},
"images_alternate":{
    "urlimage1":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/t_10136069_003.jpg",
    "urlimage2":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/m_10136069_003.jpg",
    "urlimage3":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/s_10136069_003.jpg",
    "urlimage4":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/g_10136069_003.jpg",
    "urlimage5":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/l_10136069_003.jpg",
    "urlimage6":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/t_10136069_001.jpg",
    "urlimage7":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/m_10136069_001.jpg",
    "urlimage8":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/s_10136069_001.jpg",
    "urlimage9":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/g_10136069_001.jpg",
    "urlimage10":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/l_10136069_001.jpg",
    "urlimage11":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/t_10136069_006.jpg",
    "urlimage12":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/m_10136069_006.jpg",
    "urlimage13":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/s_10136069_006.jpg",
    "urlimage14":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/g_10136069_006.jpg",
    "urlimage15":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/l_10136069_006.jpg",
    "urlimage16":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/t_10136069_005.jpg",
    "urlimage17":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/m_10136069_005.jpg",
    "urlimage18":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/s_10136069_005.jpg",
    "urlimage19":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/g_10136069_005.jpg",
    "urlimage20":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/l_10136069_005.jpg",
    "urlimage21":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/t_10136069_002.jpg",
    "urlimage22":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/m_10136069_002.jpg",
    "urlimage23":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/s_10136069_002.jpg",
    "urlimage24":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/g_10136069_002.jpg",
    "urlimage25":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/l_10136069_002.jpg",
    "urlimage26":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/t_10136069_004.jpg",
    "urlimage27":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/m_10136069_004.jpg",
    "urlimage28":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/s_10136069_004.jpg",
    "urlimage29":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/g_10136069_004.jpg",
    "urlimage30":"https://brain-images-ssl.cdn.dixons.com/9/6/10136069/l_10136069_004.jpg"
},
"universe":{
    "#cdata-section":"Household Appliances"
},
"universeid":"35",
"category":{
    "#cdata-section":"Small kitchen appliances"
},
"categoryid":"336",
"market":{
    "#cdata-section":"Small cooking appliances"
},
"marketid":"3155",
"segment":{
    "#cdata-section":"Multicookers"
},
"segmentid":"31992",
"MPN":{
    "#cdata-section":"000000859792415020"
},
"EAN":"5413184100599",
"color":null,
"weight":"1"
}
}

我唯一感兴趣的部分是infotech,所以我尝试使用"product.infotech.#cdata-section"的路径查询来获取该部分,如下所示:

代码语言:javascript
复制
var json = JObject.Parse(response);
var products = json.SelectToken(path);

这给了我这样的结论:

代码语言:javascript
复制
{
    [
        {
            "Title":"OVERVIEW",
            "Priority":1,
            "Attributes":[
                {
                "Title":"Type",
                "Value":"Multicooker",
                "Priority":0
                },
                {
                "Title":"Power",
                "Value":"1500 W",
                "Priority":2
                },
                {
                "Title":"Capacity",
                "Value":"4.5 litres",
                "Priority":1
                }
            ]
        },
        {
            "Title":"FUNCTIONS",
            "Priority":2,
            "Attributes":[
                {
                "Title":"Number of programmes",
                "Value":"25",
                "Priority":4
                },
                {
                "Title":"Timer",
                "Value":"Yes",
                "Priority":8
                },
                {
                "Title":"Number of heat settings",
                "Value":"Up to 140�C",
                "Priority":7
                },
                {
                "Title":"Keep warm function",
                "Value":"Yes",
                "Priority":6
                },
                {
                "Title":"Programs",
                "Value":"- Boil- Fry- Stew- Steam- Puree- Dough",
                "Priority":5
                }
            ]
        },
        {
            "Title":"FEATURES",
            "Priority":3,
            "Attributes":[
                {
                "Title":"Digital display",
                "Value":"Yes",
                "Priority":10
                },
                {
                "Title":"Automatic shut-off",
                "Value":"Yes",
                "Priority":18
                },
                {
                "Title":"Dishwasher safe parts",
                "Value":"Yes",
                "Priority":17
                },
                {
                "Title":"Microwave safe pot",
                "Value":"No",
                "Priority":16
                },
                {
                "Title":"Oven safe pot",
                "Value":"No",
                "Priority":15
                },
                {
                "Title":"Number of tiers",
                "Value":"3",
                "Priority":14
                },
                {
                "Title":"Sound signal",
                "Value":"Yes",
                "Priority":12
                },
                {
                "Title":"Power indicator",
                "Value":"Yes",
                "Priority":11
                }
            ]
        },
        {
            "Title":"GENERAL",
            "Priority":4,
            "Attributes":[
                {
                "Title":"Colour",
                "Value":"Black",
                "Priority":20
                },
                {
                "Title":"Manufacturer's guarantee",
                "Value":"3 years",
                "Priority":26
                },
                {
                "Title":"Weight",
                "Value":"10.14 kg",
                "Priority":25
                },
                {
                "Title":"Dimensions ",
                "Value":"411 x 314 x 342 mm (H x W x D)",
                "Priority":24
                },
                {
                "Title":"Power cord length",
                "Value":"1 m",
                "Priority":23
                },
                {
                "Title":"Power cord storage",
                "Value":"No",
                "Priority":22
                },
                {
                "Title":"Box contents",
                "Value":"- StirAssist- Egg whip- Dough blade- Multiblade- Upper and lower steam baskets with lid- Inner basket- Mini bowl and mini multiblade- Measuring cup- Free cookbook",
                "Priority":21
                }
            ]
        }
    ]
}

我想将它转换为IList<JObject>,但无论我如何尝试,我都会得到一个错误。有人能帮我吗?

另外,如果我看一下产品的价值,它会显示一个数组:

EN

回答 1

Stack Overflow用户

发布于 2019-08-08 23:05:18

我认为问题在于product.infotech.#cdata-section的值是一个JSON字符串;它还不是一个数组。您可以在调试器屏幕截图中看到这一点:Type被列为String (而不是Array)。可能最初的XML在那个标记中有JSON。当整个东西被转换成JSON时,已经存在的JSON被转义,这就是为什么所有额外的反斜杠都存在。因此,要将值返回到可用的形式,您需要重新解析该部分:

代码语言:javascript
复制
var json = JObject.Parse(response);

// get the escaped JSON as a string
var productsJson = (string)json.SelectToken("product.infotech.#cdata-section");

// re-parse it to a JArray
var productsArray = JArray.Parse(productsJson);

// Now you can iterate over the array to access the JObjects within it
foreach (JObject item in productsArray)
{
    ...
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57412617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档