问题
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