简体   繁体   中英

Extract Values from JObject

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

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>()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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