Home > database >  ASP.NET convert part of JSON file to JArray
ASP.NET convert part of JSON file to JArray

Time:05-05

I have this JSON that I'm trying to parse with Newtonsoft and convert to an array:

{
"generatedAt": "2022-01-01 02:03:25",
"myitems": [
{
    "id": "1795089",
    "location": {
        "name": "Myplace",
        "countryCode": "DE"
    },
}
,
{
    "id": "1795070",
    "location": {
        "name": "Roseplace",
        "countryCode": "US"
    },
}

],
"count": 2
}

What I tried:

Dim jsonArray As Newtonsoft.Json.Linq.JArray
jsonArray = JArray.Parse(json)

But this does not find the myitems array.

So then I tried:

Dim obj As JObject = JObject.Parse(json)
Dim result As JObject = DirectCast(obj("myitems"), JObject)

But I don't know if this is the recommended approach or how to convert that object to a JArray.

CodePudding user response:

jarray.parse requires an array source...However

Dim jstring as string = "{""generatedAt"": ""2022-01-01 02:03:25"",""myitems"": [{""id"": ""1795089"",""location"":{""name"":""Myplace"",""countryCode"":""DE""},},{""id"":""1795070"",""location"": {""name"": ""Roseplace"",""countryCode"": ""US""},}],""count"": 2}"
Dim jo = Newtonsoft.Json.Linq.JObject.Parse(jstring)
Dim ja = Newtonsoft.Json.Linq.JArray.Parse(jo("myitems").toString)

But once you parse the object, do you need to parse the array?

  • Related