使用JObject,JToken和JArray进行JSON.NET解析 [英] JSON.NET Parse with JObject, JToken and JArray

查看:953
本文介绍了使用JObject,JToken和JArray进行JSON.NET解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON字符串,我想用JSON.net解析,我想循环并使用komponent数组中的名称.这是我的json字符串:

I have a json string that i'm trying to parse with JSON.net, i want to loop and use the names in the komponent array. This is my json string:

{"Name": "Service","jsonTEMPLATE": "{"komponent": [{"name": "aa"}, {"name": "bb"}]}"}

这是我使用JSON.net的代码

This is my code using JSON.net

    JObject o = JObject.Parse(serviceData);
    JToken j = (JToken)o.SelectToken("jsonTEMPLATE");
    JArray a = (JArray)j.SelectToken("komponent");

    foreach (JObject obj in a)
    {
        //Do something
    }

我从(JArray)j.SelectToken("komponent");

我在做什么错了?

推荐答案

您的JSON无效.您可以通过 JSONLint.com 对其进行检查.您在jsonTEMPLATE属性的值两边加上了引号,如果将其解释为对象,则不应该使用引号:

Your JSON is invalid. You can run it through JSONLint.com to check it. You have quotes around the value of the jsonTEMPLATE property, which should not be there if it is to interpretted as an object:

{
    "Name": "Service",
    "jsonTEMPLATE": "{"komponent": [{"name": "aa"}, {"name": "bb"}]}"
}

要使代码成功,JSON需要看起来像这样:

The JSON needs to look like this for your code to succeed:

{
    "Name": "Service",
    "jsonTEMPLATE": {"komponent": [{"name": "aa"}, {"name": "bb"}]}
}

这篇关于使用JObject,JToken和JArray进行JSON.NET解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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