Android Question JSON Parsing error,Can someone please take a look

lordor

Member
Can someone please take a look, thank you




json file is


{"tvset": {
"topic": "a/update",
"sectet": "test",
"head": "wei",
"id": "文件",
"value": "文件",
"keyboard": {
"menuitem": [
{"value": "新建","onclick": "CreateNewDoc()"},
{"value": "打开","onclick": "OpenDoc()"},
{"value": "关闭","onclick": "CloseDoc()"}
]
}
}
}


B4A
B4X:
Dim JSON As JSONParser
    Dim Map1 As Map
    JSON.Initialize(File.ReadString(File.DirAssets, "example.json"))
    Map1 = JSON.NextObject
    Dim m As Map 'helper map for navigating
    Dim MenuItems As List
    MenuItems.Initialize
    m = Map1.Get("tvset")
    m = m.Get("keyboad")
    MenuItems = m.Get("menuitem")
    For i = 0 To MenuItems.Size - 1
        m = MenuItems.Get(i)
        Log(m.Get("value"))
    Next


error log
** Activity (main) Create, isFirst = true **
Error occurred on line: 32 (Main)
java.lang.RuntimeException: Object should first be initialized (Map).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:67)
at anywheresoftware.b4a.objects.collections.Map.Get(Map.java:64)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.samples.json.main.afterFirstLayout(main.java:105)
at anywheresoftware.b4a.samples.json.main.access$000(main.java:17)
at anywheresoftware.b4a.samples.json.main$WaitForLayout.run(main.java:83)
 

Attachments

  • code-JSON.zip
    56.5 KB · Views: 186

DonManfred

Expert
Licensed User
Longtime User
B4X:
Dim parser As JSONParser
parser.Initialize(File.ReadString(File.DirAssets, "example.json"))
Dim root As Map = parser.NextObject
Dim tvset As Map = root.Get("tvset")
Dim head As String = tvset.Get("head")
Dim keyboard As Map = tvset.Get("keyboard")
Dim menuitem As List = keyboard.Get("menuitem")
For Each colmenuitem As Map In menuitem
Dim onclick As String = colmenuitem.Get("onclick")
Dim value As String = colmenuitem.Get("value")
Next
Dim topic As String = tvset.Get("topic")
Dim sectet As String = tvset.Get("sectet")
Dim id As String = tvset.Get("id")
Dim value As String = tvset.Get("value")

Helpful ressource: http://basic4ppc.com:51042/json/index.html
 
Upvote 0
Top