F Falcon Member Licensed User Aug 5, 2020 #1 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. How can I fix this? Regards, Jacques.
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. How can I fix this? Regards, Jacques.
DonManfred Expert Licensed User Longtime User Aug 5, 2020 #2 Falcon said: 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> Click to expand... 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
Falcon said: 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> Click to expand... 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?
Erel B4X founder Staff member Licensed User Longtime User Aug 5, 2020 #3 Falcon said: I get the following error, see the attached image. Click to expand... Please post logs as text, not images. You can use Xml2Map to parse the XML, get the JSON string and then parse it. Upvote 0
Falcon said: I get the following error, see the attached image. Click to expand... Please post logs as text, not images. You can use Xml2Map to parse the XML, get the JSON string and then parse it.
F Falcon Member Licensed User Aug 5, 2020 #4 Hi Erel, Sorry for my ignorance, but how do I do in code what you suggested? Upvote 0
DonManfred Expert Licensed User Longtime User Aug 5, 2020 #5 [B4X] Xml2Map - Simple way to parse XML documents Nobody likes to parse XML. Parsing JSON is simple and fun. Parsing XML is tedious and boring. That is the reason behind the Xml2Map class. It internally parses the XML document and returns a Map with the parsed data. It is similar to parsing JSON. Tip: You can use this tool to help you with... www.b4x.com Upvote 0
[B4X] Xml2Map - Simple way to parse XML documents Nobody likes to parse XML. Parsing JSON is simple and fun. Parsing XML is tedious and boring. That is the reason behind the Xml2Map class. It internally parses the XML document and returns a Map with the parsed data. It is similar to parsing JSON. Tip: You can use this tool to help you with... www.b4x.com
F Falcon Member Licensed User Aug 5, 2020 #6 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
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.
DonManfred Expert Licensed User Longtime User Aug 5, 2020 #7 Falcon said: I managed to change the Web Service so that it now returns the following, which looks more like JSON and not XML Click to expand... Correct. This is json. Use this Tool (Bookmark it) to help you find the Source needed http://basic4ppc.com:51042/json/index.html 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
Falcon said: I managed to change the Web Service so that it now returns the following, which looks more like JSON and not XML Click to expand... Correct. This is json. Use this Tool (Bookmark it) to help you find the Source needed http://basic4ppc.com:51042/json/index.html 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
F Falcon Member Licensed User Aug 5, 2020 #8 Aah! That is absolutely brilliant! Thank you so much Erel & Don for the help! You guys are awesome! Upvote 0