Android Question Problem parsing JSON format

Falcon

Member
Licensed User
Hi Guys,

I have a Web Service that returns the following JSON:

<string xmlns="http://golfanywhere.co.za/WebService/">{ "Table": [ { "id_Country": 1,'"nvcCountry": "Australia" }, { "id_Country": 2, "nvcCountry": "United States" } ] }</string>

The problem is that the formatting of the returned JSON causes an error when I try to parse it like so:
B4X:
Dim M as Map
M=parser.NextObject

I get the following error, see the attached image.

Capture.GIF


How can I fix this?

Regards,
Jacques.
 

DonManfred

Expert
Licensed User
Longtime User
I have a Web Service that returns the following JSON:
<string xmlns="http://golfanywhere.co.za/WebService/">{ "Table": [ { "id_Country": 1,'"nvcCountry": "Australia" }, { "id_Country": 2, "nvcCountry": "United States" } ] }</string>
The return is not JSON, it is XML. The JSON is inside the XML.
This is the JSON
B4X:
{ "Table": [ { "id_Country": 1,'"nvcCountry": "Australia" }, { "id_Country": 2, "nvcCountry": "United States" } ] }
You need to extract the json from the XML first. Or maybe you can configure the service to return JSON directly?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
 
Upvote 0

Falcon

Member
Licensed User
Hi Guys,

Thanks for the help so far, much appreciated! However, I am still struggling here.

I managed to change the Web Service so that it now returns the following, which looks more like JSON and not XML:
{"Table":[{"id_Country":1,"nvcCountry":"Australia"},{"id_Country":2,"nvcCountry":"United States"}]}

There are 2 records here, now the problem I have is that I do not know how to extract values out of here, I need the 'id_Country' and 'nvcCountry' data for each record.
All I have so far is (see below) and I do not know how to proceed from here?

B4X:
strJSON = job.GetString
parser.Initialize(strJSON)
Dim root_ As Map = parser.NextObject

Please could someone help?

Thank in advance,
Regards,
Jacques.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I managed to change the Web Service so that it now returns the following, which looks more like JSON and not XML
Correct. This is json.

Use this Tool (Bookmark it) to help you find the Source needed


This is the code the tool generates... You need to replace <text> with your json data.
Probably your String strJSON.

B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As Map = parser.NextObject
Dim Table As List = root.Get("Table")
For Each colTable As Map In Table
Dim nvcCountry As String = colTable.Get("nvcCountry")
Dim id_Country As Int = colTable.Get("id_Country")
Next
 
Upvote 0
Top