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 simplest way to get a value from LINQ to JSON is to use the Item[Object] index on JObject/JArray and then cast the returned JValue to the type you want. JObject/JArray can also be queried using LINQ.
The method JObject. Parse() is a JObject class method. This parse method is used to parse a JSON string into a C# object. It parses the data of string based on its key value. This key value is then used to retrieve the data.
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>()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With