本文介绍了使用JObject,JToken和JArray进行JSON.NET解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个JSON字符串,我想用JSON解析,我想循环并使用komponent数组中的名称.这是我的json字符串:
I have a json string that i'm trying to parse with JSON, 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的代码
This is my code using JSON
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 对其进行检查.您在jsonTEMPLATE属性的值两边加上了引号,如果将其解释为对象,则不应该使用引号:
Your JSON is invalid. You can run it through JSONLint 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解析
发布评论