0

I have an vb.net like the next:

Public Shared Function GetCountries()

    Dim lCheckOutManager As CheckOutManager = CType(HttpContext.Current.Session("CHECKOUT_MANAGER"), CheckOutManager)
    Dim dataResult = lCheckOutManager.getCountries()
    Return JsonConvert.SerializeObject(dataResult, Formatting.Indented)

End Function

Which returns a Json object to javascript like the next:

[
   {
      "ID":12,
      "NAME":"Canada",
      "INCOTERM":3
   },
   {
      "ID":60,
      "NAME":"United States",
      "INCOTERM":3
   }
]

And as I'm needing to join two json in the response and be able to access them separately, I did a little modification to my method like this:

Public Shared Function GetCountries()

    Dim lCheckOutManager As CheckOutManager = CType(HttpContext.Current.Session("CHECKOUT_MANAGER"), CheckOutManager)

    Dim dataResult = lCheckOutManager.getCountries()
    Dim strDataResult As String = "{Countries:" & JsonConvert.SerializeObject(dataResult, Formatting.Indented) & "}"

    Return JsonConvert.SerializeObject(strDataResult, Formatting.Indented)

End Function

So I can identify the first json by the node "Countries" (and then I will concatenate the second JSON), so I get a json like the next:

{
   "Countries":[
      {
         "ID":12,
         "NAME":"Canada",
         "INCOTERM":3
      },
      {
         "ID":60,
         "NAME":"United States",
         "INCOTERM":3
      }
   ]
}

But all my attempts to retrieve the "Countries" node are

being unsuccessful. Iv'e tried:

JSON.parse(results[0].d).Countries, JSON.parse(results[0].d)["Countries"], JSON.parse(results[0].d)[0], Object.keys(JSON.parse(results[0].d))[0] but none of them works, as they end in undefined or empty strings.

Any help?

Diego Perez
  • 1,493
  • 1
  • 24
  • 43
  • you can access them like this `result.Countries`, for example ( `result.Countries[0].NAME` ) will return `Canada` or ( `result.Countries[1].NAME` ) will return `United States` – Alireza May 25 '22 at 09:25
  • Thanks for your reply @Alireza. results[0].d => ""{Countries:[\r\n {\r\n \"ID\": 12,\r\n \"NAME\": \"Canada\",\r\n \"INCOTERM\": 3\r\n },\r\n {\r\n \"ID\": 60,\r\n \"NAME\": \"United States\",\r\n \"INCOTERM\": 3\r\n }\r\n]}"", but results.Countries => undefined. – Diego Perez May 25 '22 at 09:51
  • if your returning result is a `string`, in order to access the data you must use `JSON.parse();` to convert it to an `object`, The code you've put up there is aIready an object no need to parse it. check this: `let text = '{"Countries":[{"ID": 12,"NAME": "Canada","INCOTERM": 3},{"ID": 60,"NAME": "United States","INCOTERM": 3}]}'; console.log(JSON.parse(text).Countries[0].NAME); let object = {Countries:[{ID: 12,NAME: "Canada",INCOTERM: 3},{ID: 60,NAME: "United States",INCOTERM: 3}]}; console.log(object.Countries[0].NAME);` – Alireza May 25 '22 at 10:20

0 Answers0