如何解析json数组json.net [英] How to parse a json array json.net

查看:90
本文介绍了如何解析json数组json.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这听起来很基础,但是围绕这个问题的所有答案都是愚蠢的,笨拙的代码,不允许我使用所需的功能.我需要解析这个json数组.

I know it sounds basic but all answers around this question have been stupidly large and bulky code that does not allow the functionality i need. i need to parse this json array.

[
   {
      "label":"Cow (1)",
      "value":3309
   },
   {
      "label":"Cow (1)",
      "value":14998
   },
   {
      "label":"Cow (4)",
      "value":20969
   },
   {
      "label":"Cow (4)",
      "value":20970
   },
   {
      "label":"Cow (4)",
      "value":20971
   },
   {
      "label":"Cowardly Bandit",
      "value":1886
   },
   {
      "label":"Cow calf (1)",
      "value":2310
   },
   {
      "label":"Coward in armour (82)",
      "value":5097
   },
   {
      "label":"Coward with bow (105)",
      "value":6049
   },
   {
      "label":"Cow calf (1)",
      "value":20979
   },
   {
      "label":"Undead cow (4)",
      "value":1691
   },
   {
      "label":"Plague cow",
      "value":1998
   },
   {
      "label":"Plague cow",
      "value":1999
   },
   {
      "label":"Unicow (57)",
      "value":5603
   },
   {
      "label":"Zombie cow (1)",
      "value":18597
   },
   {
      "label":"Zombie cow (1)",
      "value":20928
   },
   {
      "label":"Super Cow (5)",
      "value":21497
   },
   {
      "label":"Dairy cow",
      "value":22418
   },
   {
      "label":"Armoured cow thing (62)",
      "value":5986
   },
   {
      "label":"Armoured cow thing (62)",
      "value":6048
   }
]

当我尝试访问数组内的数据点时,它返回null,代码:

And when i try to access the data point inside the array it returns null, code:

Stream stream = client.OpenRead("http://services.runescape.com/m=itemdb_rs/bestiary/beastSearch.json?term=" + Input);
StreamReader reader = new StreamReader(stream);
jObject = JObject.Parse(reader.ReadToEnd());
stream.Close();
//put items into list view
// i is the number where the json object is in the array 
var lvi = new ListViewItem(new string[] { (string)jObject[i]["label"], (string)jObject[i]["value"] }); 

我不想使用课程

推荐答案

不想使用类很奇怪,但并非不可能.

Not wanting to use classes is weird but not impossible.

var json = reader.ReadToEnd();
var objects = JsonConvert.DeserializeObject<dynamic[]>(json);
var lvi = new ListViewItem(new string[] { (string)objects[i].label, (string)objects[i].value }); 

这篇关于如何解析json数组json.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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