简体   繁体   English

如何在此JSON字符串的数组中获取属性?

[英]How do I get properties in the array in this JSON string?

In the JSON string below, how do I access the values of the "at" and "current_value" properties within the "datastreams" array ? 在下面的JSON字符串中,如何访问“数据流”数组中的“ at”和“ current_value”属性的值?

In this example, there is only one datastream but in reality there could be many. 在此示例中,只有一个数据流,但实际上可能有很多。 I need to access the datastream by "id" property. 我需要通过“ id”属性访问数据流。 Once I figure out this issue, I plan to use a where clause with the id == to the id of the desired datastream. 一旦确定了这个问题,我计划使用id ==的where子句作为所需数据流的id。

I tried using the approach discussed here , under "JSON in Windows 8 – A Simpler Approach" but it's not working. 我尝试使用此处讨论的“ Windows 8中的JSON –一种更简单的方法”下的方法,但是它不起作用。

In this code, json contains the JSON returned from the service I'm calling. 在此代码中, json包含从我正在调用的服务返回的JSON。 prop is populated with a JsonArray . prop中填充了JsonArray current results in an exception with an inner message of "JSON value not found" current导致内部消息为“找不到JSON值”的异常

var json = JsonObject.Parse(responseBodyAsText);
var prop = json.GetNamedArray("datastreams");

var current = from p in prop
    select new
    {
       datastream = p.GetObject().GetNamedString("datastreams"),
       datetime = p.GetObject().GetNamedString("at"),
       value = p.GetObject().GetNamedString("current_value")
    };

Here is the JSON string: 这是JSON字符串:

{
   "title":"X",
   "status":"X",
   "creator":"X",
   "datastreams":
      [
         {
            "at":"x",
            "max_value":"X",
            "current_value":"X",
            "id":"X",
            "min_value":"X"
         }
      ],
   "location":{"exposure":"x","domain":"x","disposition":"x","lat":X,"lon":-X},
   "created":"X",
   "tags":["X"],
   "feed":"X",
   "private":"X",
   "id":X,
   "description":"X",
   "version":"X",
   "updated":"X"
}

datastream = p.GetObject().GetNamedString("datastreams") datastream = p.GetObject()。GetNamedString(“ datastreams”)

The code above should return an array of objects. 上面的代码应返回一个对象数组。 You'll need to loop through the array of objects to check the value of each object's "at" and "current_value" properties. 您将需要遍历对象数组以检查每个对象的“ at”和“ current_value”属性的值。

datastream return an array as you can notice by [ ] so: 您可以通过[]注意到数据流返回一个数组,因此:

datetime = datastream[0].at
value = datastream[0].current_value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM