B4J Tutorial [Server] Online Json Tree Example

This example is a port of the following desktop example: http://www.b4x.com/android/forum/threads/jsontree-tool-to-help-with-json-parsing-b4a-b4j.35963/

SS-2014-03-20_11.43.42.png


You can try it online: https://b4x.com:51041/json/index.html

The server parses the JSON string and returns the tree representation and also the B4A / B4J code you need in order to parse it.

The code is similar to the desktop solution code. The main difference is that the tree is built with standard HTML lists, so instead of adding the elements to a real TreeView we need to build a string.

The tree style is based on this blog: http://odyniec.net/articles/turning-lists-into-trees/
The HTML divs are scrollable. This is done with this plugin: http://www.jacklmoore.com/autosize/
 

Attachments

  • ServerJsonTree.zip
    7.7 KB · Views: 1,663
Last edited:

Peter Simpson

Expert
Licensed User
Longtime User
This is absolutely awesome Erel,
I use the JsonTree via B4J(which produces the same code) and it has helped me out a lot. So much so that I was able to integrate a new weather feed from http://openweathermap.org into a test app and tidy everything up in less than 40 minutes :) Usually that would have taken me a good few hours, so thank you very much. I've now used your JsonTree on 4 different Json feeds, it worked 100% of the time, every time.

P.S. I do not recommend that anybody uses the weather data from http://openweathermap.org as it is seriously seriously seriously incorrect. But Erel's Json feed examples work flawlessly...

Below is a weather feed using Erels JsonTree examples:
Untitled-2.jpg


Untitled-1.jpg


Thanks again for your hard work and dedication to the cause Mr U...
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User

Hi @Erel this is a great tool! I wished i had this at the begining of my B4A-learning...

TWO issues;

1.
The code created is wrong

B4X:
parser.Initialize(<text>))

If i replace <text> with the json (or to see just replace it with an X

B4X:
parser.Initialize(X))
There is one ) too much :)

2.
When i copy the created code to b4a all lines with an indent are not parseable from b4a... I manually have to remove the indent and reset it again to b4a to work parsing this line...

BOTH Issues are only with the online-tool. The Desktop Json-Tree dont have this issues.
 
Last edited:

Sam H

Member
Licensed User
Longtime User
I am confused about the for each loop that is generated in the example. Aren't the previous values of "value" and "onclick" lost with each iteration of the loop. I would have imagined the for each loop would add to a list each time rather than re-assign the values of "value" and "onclick".

Am I missing something?

Thanks
 

Sam H

Member
Licensed User
Longtime User
This is only a template for you to start with. If you want to add the values to a list then you will need to add the code that adds them to a list.
How do I access the items created by the loop before the last iterations? Or is not possible, without altering the code?
 

Sam H

Member
Licensed User
Longtime User
json parser error.png


When parsing my json file using the online tool, my headings ("LOCA_ID", "ISPT_TOP", ISPT_SEAT" ... etc) where not parsed in this order. As you can see from the tree view generated in the above picture, the order was "ISPT_HAM", "ISPT_WAT", "ISPT_MAIN" ...etc.

1. how is the order generated? ( it isn't alphabetical, and it isn't random)

2. Is there a way to force it to parse it in order?

3. Is the solution to enclose the headings in square brackets [] and have it parse each value of the list?
 

Sam H

Member
Licensed User
Longtime User
I have got the json parsing working on my tablet (Android Jelly Bean) perfectly, however I just ran it on my phone (Android Gingerbread), and I got the error message:

"java.lang.RuntimeException: JSON Object expected." on the line:

Dim root As Map = Parser.NextObject

Parser is initialised as
"Parser.Initialize(configString)", and the debugger shows that configString is the value I expect it to be.

Any Ideas?

Thanks

Sam
 

Phayao

Active Member
Licensed User
Longtime User
Hello,

another silly question from the beginner:
I want to create a JSON string to pass to javascript in a webapp.
So that would be the opposite of the json-parser here - I am not able to handle single and double quotes.
The json string had to be like: '[{"x":218,"y":209,"WPname_en":"turn right"},{"x":683,"y":216,"WPname_en":"go right"}]'
with each item enclosed in double quotes. I could not create such a string using stringbuilder, because we cannot use something like sb.Append(""") or sb.Append("\""). Even I tried: sb.Append("&quot;"), but the ascii code is not recognized by the javascript function JSON.parse(...)

Anybody has an idea ? I guess it must be trivial but i cannot figure it out now.
Thanks a million,

Chris
 

Roycefer

Well-Known Member
Licensed User
Longtime User
B4X allows for double quotes in String literals by putting in two double quotes:
B4X:
Dim quote As String = "a""b"    'This will be the String: a"b
 

Phayao

Active Member
Licensed User
Longtime User
thanks a lot, that did the trick
my solution until then was to usse single quotes and on the javascript side replace them by double quotes:
with: jsonData = jsonData.replace(/'/g, '"');
But your solution is more elegant, thank you.
 
Top