I'm trying to parse a JSON file but I'm running into some problems.
I'm using the JSON tutorial as base for my code.
When running with the json file from the example it running okay.
But when using my own json file I get som errors.
I have a web from where I loads the json data sand save it to a file. And for that I use HttoUtils2 and that part seems to Work.
I then took my json file from the web and put it in my project as an assets file.
And heres where my problem comes. I cannot parse the json file I get an error.
My json files starts with "status" whereas the example json starts with "menu", Next I need to find "posts" in my file. Under "post" I need to find "title_plain" and under that I need to find "content"
The value from "content is what I need to get in my App.
Heres my source code:
This is from the LOG:
I have attached the json file as ZIP.
I hope someone can help me.
I'm using the JSON tutorial as base for my code.
When running with the json file from the example it running okay.
But when using my own json file I get som errors.
I have a web from where I loads the json data sand save it to a file. And for that I use HttoUtils2 and that part seems to Work.
I then took my json file from the web and put it in my project as an assets file.
And heres where my problem comes. I cannot parse the json file I get an error.
My json files starts with "status" whereas the example json starts with "menu", Next I need to find "posts" in my file. Under "post" I need to find "title_plain" and under that I need to find "content"
The value from "content is what I need to get in my App.
Heres my source code:
B4X:
#Region Module Attributes
#FullScreen: False
#IncludeTitle: True
#ApplicationLabel: HttpUtils2
#VersionCode: 1
#VersionName:
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#LibraryVersion: 2.01
#LibraryName: HttpUtils2
#End Region
'Activity module
Sub Process_Globals
End Sub
Sub Globals
End Sub
Sub Activity_Create(FirstTime As Boolean)
'** Here I call the parse SUB with the name of my assets file
ParseJSONFile("nyheder.json")
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub ParseJSONFile(JSONFile As String)
Dim JSON As JSONParser
Dim Map1 As Map
JSON.Initialize(File.ReadString(File.DirAssets , JSONFile))
Map1 = JSON.NextObject
Dim m As Map 'helper map for navigating
Dim MenuItems As List
m = Map1.Get("status")
m = m.Get("posts")
MenuItems = m.Get("title_plain")
For i = 0 To MenuItems.Size - 1
m = MenuItems.Get(i)
Log(m.Get("content"))
Next
End Sub
This is from the LOG:
PackageAdded: package:anywheresoftware.b4a.samples.httputils2
** Activity (main) Create, isFirst = true **
main_parsejsonfile (B4A line: 75)
m = Map1.Get("status")
java.lang.ClassCastException: java.lang.String cannot be cast to anywheresoftware.b4a.objects.collections.Map$MyMap
at anywheresoftware.b4a.samples.httputils2.main._parsejsonfile(main.java:460)
at anywheresoftware.b4a.samples.httputils2.main._activity_create(main.java:300)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.samples.httputils2.main.afterFirstLayout(main.java:98)
at anywheresoftware.b4a.samples.httputils2.main.access$100(main.java:16)
at anywheresoftware.b4a.samples.httputils2.main$WaitForLayout.run(main.java:76)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4867)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)
I have attached the json file as ZIP.
I hope someone can help me.