栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

c# jarray 长度

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c# jarray 长度

How can I get the length of a JSON Array I get using json.net in C#? After sending a SOAP call I get a JSON string as answer, I use json.net to parse it.

Example of the json I got:

{"JSONObject": [

{"Id":"ThisIsMyId","Value":"ThisIsMyValue"},

{"Id":"ThisIsMyId2","Value":"ThisIsMyValue2"}

]}

And I parse it and write it in console:

var test = JObject.Parse (json);

Console.WriteLine ("Id: {0} Value: {1}", (string)test["JSONObject"][0]["Id"], (string)test["JSONObject"][0]["Value"]);

This works like a spell, only I don't know the length of the JSONObject, but I need to do it in a for loop. I only have no idea how I can get the length of test["JSONObject"]

But something like test["JSONObject"].Length would be too easy I guess :(..

解决方案

You can cast the object to a JArray and then use the Count property, like so:

JArray items = (JArray)test["JSONObject"];

int length = items.Count;

You can then loop the items as follows:

for (int i = 0; i < items.Count; i++)

{

var item = (JObject)items[i];

//do something with item

}

According to Onno (OP), you can also use the following:

int length = test["JSONObject"].Count();

However, I have not personally confirmed that this will work

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/911059.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号