Other JsonTree - Tool to help with JSON parsing (B4A / B4J)

SS-2013-12-23_10.11.49.png


JsonTree is a tool that helps developers to parse and understand JSON strings.

It shows the data structure in a TreeView and it also prints the code (B4J / B4A) required in order to extract the data from the string.

Note that variables in the generated code are based on the JSON fields. It may produce invalid identifiers. However it should be quite simple to take the generated code and use it in your app.

The compiled jar can be downloaded here: www.b4x.com/b4j/files/JsonTree.jar

An online version of this tool is available here: http://basic4ppc.com:51042/json/index.html

The source code is attached.

Updated project: https://www.b4x.com/android/forum/t...with-json-parsing-updated-as-23-4-2022.140068
 

Attachments

  • JsonTree.zip
    3.8 KB · Views: 3,561
Last edited:

NeoTechni

Well-Known Member
Licensed User
Longtime User
Will you be integrating it into B4A? Like adding a shortcut to it in a menu somewhere
 

NJDude

Expert
Licensed User
Longtime User
The code fails to get results if you have a json like this:
B4X:
[
  "Item1",
  "Item2",
  "Item3",
  "Item4"
]
Maybe if just an array is detected omit adding the "For Each" loop to the code.
 
Last edited:

hookshy

Well-Known Member
Licensed User
Longtime User
I try to parse json string from glosbe witch is free tool dictionary and I could not understand where I am doing wrong .
I am getting error ..object should be first initialized list. I tried to initialize the components but no result. Sometimes it works sometimes not.
I use the folowing code
I do a http utils job to retrive job.getstring from http://glosbe.com/gapi/translate?from=ron&dest=eng&format=json&phrase=casa

B4X:
Sub parse_translation(detail As String)
'http://glosbe.com/gapi/translate?from=ron&dest=eng&format=json&phrase=casa
Dim sb As StringBuilder
sb.Initialize

Dim parser As JSONParser
parser.Initialize(detail)

Dim root As Map = parser.NextObject
'Dim authors As Map = root.Get("authors")
'Dim 2698 As Map = authors.Get("2698")
'Dim id As Int = 2698.Get("id")
'Dim U As String = 2698.Get("U")
'Dim N As String = 2698.Get("N")
'Dim 1 As Map = authors.Get("1")
'Dim id As Int = 1.Get("id")
'Dim U As String = 1.Get("U")
'Dim N As String = 1.Get("N")
'Dim 2877 As Map = authors.Get("2877")
'Dim id As Int = 2877.Get("id")
'Dim U As String = 2877.Get("U")
'Dim N As String = 2877.Get("N")
'Dim 25018 As Map = authors.Get("25018")
'Dim id As Int = 25018.Get("id")
'Dim U As String = 25018.Get("U")
'Dim N As String = 25018.Get("N")
'Dim 25115 As Map = authors.Get("25115")
'Dim id As Int = 25115.Get("id")
'Dim U As String = 25115.Get("U")
'Dim N As String = 25115.Get("N")
'Dim result As String = root.Get("result")
'Dim dest As String = root.Get("dest")
'Dim phrase As String = root.Get("phrase")
'Dim from As String = root.Get("from")
Dim tuc As List = root.Get("tuc")

For Each coltuc As Map In tuc
'    Dim authors As List = coltuc.Get("authors")
'    For Each colauthors As Int In authors
'    Next
'   
'    Dim meaningId As String = coltuc.Get("meaningId")
'    Dim meanings As List = coltuc.Get("meanings")
'    For Each colmeanings As Map In meanings
'        Dim text As String = colmeanings.Get("text")
'        Dim language As String = colmeanings.Get("language")
'    Next
'   
    Dim phrase As Map = coltuc.Get("phrase")
    Dim text As String = phrase.Get("text")
    Dim language As String = phrase.Get("language")
    sb.Append(text).Append(CRLF)
   
msgbox(sb.tostring,"Glosbe Dictionary")
Next
end sub

** Service (httputils2service) Create **
** Service (httputils2service) Start **
JobName = job_tr, Success = true
main_parse_translation (java line: 5082)
java.lang.RuntimeException: Object should first be initialized (List).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:45)
at anywheresoftware.b4a.objects.collections.List.getSize(List.java:120)
at hs.easy.romt.main._parse_translation(main.java:5082)
at hs.easy.romt.main._jobdone(main.java:4348)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:167)
at anywheresoftware.b4a.keywords.Common$4.run(Common.java:885)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3806)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: Object should first be initialized (List).
 

PaulD

Member
Licensed User
Longtime User
This looks like a GREAT tool...

I downloaded and installed B4j just to use this tool... however,


It looks like it is looking for a lib called httpclient-4.0.jar in the libraries file

looking through the core lib files included, there is the jHTTP and jHttpUtils2 files in place

Any thoughts on this?

Solved: missing lib httpclient-4.0.jar ...
 
Last edited:
Sorry, but it doesnt run in my PC. The error said "Error parsing string: java.lang.RuntimeException: JSON array expected"
 

Attachments

  • jsonTreeError.PNG
    jsonTreeError.PNG
    49.9 KB · Views: 1,063
  • jsonTreeError.PNG
    jsonTreeError.PNG
    49.9 KB · Views: 912
Top