使用JObject,JToken和JArray进行JSON.NET解析

编程入门 行业动态 更新时间:2024-10-09 03:29:01
本文介绍了使用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解析

本文发布于:2023-11-28 20:25:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643835.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:JToken   JObject   JArray   NET   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!