Android Tutorial Android JSON tutorial

Status
Not open for further replies.

ivanomonti

Expert
Licensed User
Longtime User
Json record

B4X:
[{"a01":"Via dell'Industria  Solaro  MI,  Italia","a02":"Desidero fiore","a03":"user-male.png","a04":"45.622287822509854","a05":"9.107269731567708","a06":"1.4071125984191895","a07":"06-05-2014","a08":"15:52:50","a09":"test","r1":"0"},{"a01":"Via dell'Industria  Solaro  MI,  Italia","a02":"Desidero auto","a03":"user-female.png","a04":"45.62198194001763","a05":"9.107868433652834","a06":"0.9736074805259705","a07":"06-05-2014","a08":"21:59:13","a09":"test","r1":"0"}]

Code

B4X:
Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
    Dim s As String
    s = Response.GetString("UTF8")
  
    Log(s)
  
    Dim JSON As JSONParser
    JSON.Initialize(s)
  
    Try
        Dim Map1 As Map
        Map1 = JSON.NextObject ' error line
        ListView1.AddSingleLine(Map1.Get("display_name"))
    Catch
        Msgbox("Error",Response.GetString("UTF8"))
    End Try
    Response.Release
End Sub

Error parse


B4X:
Error occurred on line: 92 (main)

java.lang.RuntimeException: JSON Object expected.

at anywheresoftware.b4a.objects.collections.JSONParser.NextObject(JSONParser.java:47)

at java.lang.reflect.Method.invokeNative(Native Method)

at java.lang.reflect.Method.invoke(Method.java:525)

at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:485)

at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:229)

at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:174)

at java.lang.reflect.Method.invokeNative(Native Method)

at java.lang.reflect.Method.invoke(Method.java:525)

at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)

at anywheresoftware.b4a.BA$3.run(BA.java:318)

at android.os.Handler.handleCallback(Handler.java:730)

at android.os.Handler.dispatchMessage(Handler.java:92)

at android.os.Looper.loop(Looper.java:137)

at android.app.ActivityThread.main(ActivityThread.java:5493)

at java.lang.reflect.Method.invokeNative(Native Method)

at java.lang.reflect.Method.invoke(Method.java:525)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)

at dalvik.system.NativeStart.main(Native Method)

!!! :-(
 

DonManfred

Expert
Licensed User
Longtime User
B4X:
Dim parser As JSONParser
parser.Initialize(s)
Dim root As List = parser.NextArray
For Each colroot As Map In root
   Dim a01 As String = colroot.Get("a01")
   Dim a02 As String = colroot.Get("a02")
   Dim a03 As String = colroot.Get("a03")
   Dim a04 As String = colroot.Get("a04")
   Dim a05 As String = colroot.Get("a05")
   Dim a06 As String = colroot.Get("a06")
   Dim a07 As String = colroot.Get("a07")
   Dim a08 As String = colroot.Get("a08")
   Dim a09 As String = colroot.Get("a09")
   Dim r1 As String = colroot.Get("r1")
Next

BTW: There is no "display_name" in the json

PS: You can use this to get correct code for json-parse
 

little3399

Active Member
Licensed User
Longtime User
Hi ,Erel

I found a bug in Json lib , when there are member of json object it's value was null will be

cause a error "....
Error occurred on line: 238 (main)
org.json.JSONException: Expected literal value at character 109 of {ShopName=广州市越秀区中山五路219号(近人民公园南广场)动漫星城, Status=0, VisitDate=2013-5-31, RpjName=演示项目13M5, Vid=215900, Address=} " ... in this example the value of the "Address" is null

Is this a bug of JSON LIB ? tks!
 

little3399

Active Member
Licensed User
Longtime User
Not sure ,here is our original json data in attachment file (1.zip)
 

Attachments

  • 1.zip
    456 bytes · Views: 593

little3399

Active Member
Licensed User
Longtime User
Oh,Erel,I put a wrong 1.json file , and you can try this , such as let Address="" or Address=" ", and usd jp.nextobject .... , I had update the 1.zip file

Tks!

Dim JSON As JSONParser
Dim Map1 As Map

JSON.Initialize(File.ReadString(File.DirAssets, "1.json"))
Log("JSON="&JSON)
Dim List1 As List
List1.Initialize
List1=JSON.NextArray
Log(List1)
Map1.Initialize
For i=0 To List1.Size-1
Log(List1.Get(i))
JSON.Initialize(List1.Get(i))
Map1=JSON.NextObject
Log(Map1.Get("Vid"))
Log(Map1.Get("ShopName"))
Log(Map1.Get("RpjName"))
Log(Map1.Get("VisitDate"))
Log(Map1.Get("Address"))
Log(Map1.Get("Status"))

Next
 

Attachments

  • 1.zip
    460 bytes · Views: 532

little3399

Active Member
Licensed User
Longtime User
Hi, Erel
Your mean the Json method such as json.NextArray, NextObject ... only be call once ,if they call again will be cause some error ?
 

ivanomonti

Expert
Licensed User
Longtime User
error parse nextArray


B4X:
Error occurred on line: 134 (main)
java.lang.RuntimeException: JSON Array expected.
   at anywheresoftware.b4a.objects.collections.JSONParser.NextArray(JSONParser.java:62)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
   at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:302)
   at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
   at anywheresoftware.b4a.BA$3.run(BA.java:319)
   at android.os.Handler.handleCallback(Handler.java:733)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:136)
   at android.app.ActivityThread.main(ActivityThread.java:5103)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
   at dalvik.system.NativeStart.main(Native Method)

B4X:
Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
   Try
     If Response <> Null Then
       Dim s As String
       s = Response.GetString("UTF8")
   
       Dim parser As JSONParser
       parser.Initialize(s)
       Dim root As List
       root.Initialize
       root = parser.NextArray  ' < line error >

       For Each colroot As Map In root
        Module_setting.address = colroot.Get("prv")
       Next
       
       Response.Release
     End If
   Catch
     End Try
End Sub

JSon result

B4X:
{
  "results" : [
  {
  "address_components" : [
  {
  "long_name" : "Strada Provinciale 25",
  "short_name" : "SP25",
  "types" : [ "route" ]
  },
  {
  "long_name" : "Boffalora d'Adda",
  "short_name" : "Boffalora d'Adda",
  "types" : [ "administrative_area_level_3", "political" ]
  },
  {
  "long_name" : "Lodi",
  "short_name" : "LO",
  "types" : [ "administrative_area_level_2", "political" ]
  },
  {
  "long_name" : "Lombardy",
  "short_name" : "Lombardy",
  ......
 

little3399

Active Member
Licensed User
Longtime User
Your must using JSON.NextObject to handel your json data, and not Json.nextarrary ...
 

ivanomonti

Expert
Licensed User
Longtime User
B4X:
Private Sub jsonParseCategory(s As String)
   Try
     Log(s)
       Dim JSON As JSONParser
     JSON.Initialize(s)
     ListCategory = JSON.NextArray
     ToastMessageShow("Updated category list", True)
   Catch
     ToastMessageShow("Error parse json data", True)
     End Try
End Sub

B4X:
start,-a,android.intent.action.MAIN,-n,b4a.example/.main,-f,0x20000000
(Intent) Intent { act=android.intent.action.MAIN flg=0x20000000 cmp=b4a.example/.main }
no extras
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
Query: category[{"Category":"Animal","SubCategory":"Dog Sitter"},{"Category":"Animal","SubCategory":"Cat Sitter"},{"Category":"Animal","SubCategory":"Veterinary"},{"Category":"Animal","SubCategory":"Other"},{"Category":"Work","SubCategory":"Painter"},{"Category":"Work","SubCategory":"Mason"},{"Category":"Work","SubCategory":"Elettrical"},{"Category":"Work","SubCategory":"Hydraulic"},{"Category":"Work","SubCategory":"Garden"},{"Category":"Work","SubCategory":"Other"},{"Category":"Assistance","SubCategory":"Garegivers"},{"Category":"Assistance","SubCategory":"Nurse"},{"Category":"Assistance","SubCategory":"General practitioner"},{"Category":"Assistance","SubCategory":"Dentist"},{"Category":"Assistance","SubCategory":"Gynecologist"},{"Category":"Assistance","SubCategory":"Physiotherapist"},{"Category":"Advice","SubCategory":"Mediator"},{"Category":"Advice","SubCategory":"Criminal Lawyer"},{"Category":"Advice","SubCategory":"Civil lawyer"},{"Category":"Advice","SubCategory":"Noary"},{"Category":"Advice","SubCategory":"Accountant"},{"Category":"Advice","SubCategory":"Business Consultant"},{"Category":"Personal Care","SubCategory":"Hairdresser"},{"Category":"Personal Care","SubCategory":"Beautician"},{"Category":"Personal Care","SubCategory":"Personal Trainer"},{"Category":"Personal Care","SubCategory":"Masseur"},{"Category":"Personal Care","SubCategory":"masseuse"},{"Category":"Animal","SubCategory":"All"},{"Category":"Work","SubCategory":"All"},{"Category":"Assistance","SubCategory":"All"},{"Category":"Advice","SubCategory":"All"},{"Category":"Personal Care","SubCategory":"All"}]
Error occurred on line: 80 (main)
java.lang.RuntimeException: JSON Array expected.
    at anywheresoftware.b4a.objects.collections.JSONParser.NextArray(JSONParser.java:62)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:302)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
    at anywheresoftware.b4a.BA$3.run(BA.java:319)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5103)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
    at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Pause, UserClosed = false **
 

ivanomonti

Expert
Licensed User
Longtime User
solved my problem, I have to be more careful :-(

problem {Query:}

Query: category[{"Category":"Animal","SubCategory":"Dog Sitter"},{"Category":"Animal","SubCategory":"Cat Sitter"},{"Category":"Animal","SubCategory":"Veterinary"},{"Category":"Animal","SubCategory":"Other"},{"Category":"Work","SubCategory":"Painter"},{"Category":"Work","SubCategory":"Mason"},{"Category":"Work","SubCategory":"Elettrical"},{"Category":"Work","SubCategory":"Hydraulic"},{"Category":"Work","SubCategory":"Garden"},{"Category":"Work","SubCategory":"Other"},{"Category":"Assistance","SubCategory":"Garegivers"},{"Category":"Assistance","SubCategory":"Nurse"},{"Category":"Assistance","SubCategory":"General practitioner"},{"Category":"Assistance","SubCategory":"Dentist"},{"Category":"Assistance","SubCategory":"Gynecologist"},{"Category":"Assistance","SubCategory":"Physiotherapist"},{"Category":"Advice","SubCategory":"Mediator"},{"Category":"Advice","SubCategory":"Criminal Lawyer"},{"Category":"Advice","SubCategory":"Civil lawyer"},{"Category":"Advice","SubCategory":"Noary"},{"Category":"Advice","SubCategory":"Accountant"},{"Category":"Advice","SubCategory":"Business Consultant"},{"Category":"Personal Care","SubCategory":"Hairdresser"},{"Category":"Personal Care","SubCategory":"Beautician"},{"Category":"Personal Care","SubCategory":"Personal Trainer"},{"Category":"Personal Care","SubCategory":"Masseur"},{"Category":"Personal Care","SubCategory":"masseuse"},{"Category":"Animal","SubCategory":"All"},{"Category":"Work","SubCategory":"All"},{"Category":"Assistance","SubCategory":"All"},{"Category":"Advice","SubCategory":"All"},{"Category":"Personal Care","SubCategory":"All"}]
 

bluedude

Well-Known Member
Licensed User
Longtime User
How can we create something like below in code:

"checked": {
"computed": {
"address": [
"Line1",
"Line2",
"Line3"
],
"test": 1.4
},
},

Need to be able to create object arrays without keys etc.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…