Extract Values from JObject

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-08 16:29:55

问题


I'm trying to extract some values from a Json but I have problems with the data between [ ]

{ 
  attrib1: ""es-BO"",
  attrib2: 2,
  Segment: [
  {
    inAttrib1: ""value1"",
    inAttrib2: ""value2"",
    inAttrib3: ""value3""
  }]
}

for the first values I'm using:

string attrib1 = request.GetValue("attrib1").Value<string>();
.
.
.

but when I'm trying to do:

string inAttrib1 = request.GetValue("inAttrib1").Value<string>();

doesn't work...what can I do?, or exists another way to do the same


回答1:


The data between (and including) [] is called an array. Before moving on it might be helpful to look at JSON's home page, specifically at the different data types available.

You need to navigate down to the Segment array, then get the first element, then that element's inAttrib1 property:

string attrib1Value = request["Segment"][0]["inAttrib1"].Value<string>();

Or alternatively:

string attrib1Value = request.SelectToken(@"Segment[0].inAttrib1").Value<string>()


来源:https://stackoverflow.com/questions/25493951/extract-values-from-jobject

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!