Json file error or code Basic4android ?

luiswagnersantos

Member
Licensed User
Longtime User
friends
I'm learning how to use JSON for data transfer
Build a server to provide data.
The JSON string is correct?

{"result":[[{"CODIGO":"1 ","NOME":"PLASTICOS"},{"CODIGO":"2 ","NOME":"VIDROS"},{"CODIGO":"3 ","NOME":"BRINQUEDOS"},{"CODIGO":"4 ","NOME":"IMPORTADOS"},{"CODIGO":"5 ","NOME":"METAIS"},{"CODIGO":"6 ","NOME":"PAPELARIA"},{"CODIGO":"7 ","NOME":"T\u00C9RMICOS"},{"CODIGO":"8 ","NOME":"DIVERSOS"}]]}

My code for read and show the log ( very simple )

PHP:
Dim JSON As JSONParser
Dim Map1 As Map

JSON.Initialize(File.ReadString(File.DirDefaultExternal, "marana.json"))
Map1 = JSON.NextObject
Dim m As Map 'helper map for navigating
Dim MenuItems As List

m = Map1.Get("result")
m = m.Get("")
MenuItems = m.Get("")

For i = 0 To MenuItems.Size - 1
    m = MenuItems.Get(i)
    Log(m.Get("value"))
Next

My Server work

serverjson.jpg


Someone could send me some tips
thank you
:D
 

luiswagnersantos

Member
Licensed User
Longtime User
Hi Martin.

thanks for your answer

I auditioned on the site that sent me and the result is this:

Valid Json

What can be?

My Json File

PHP:
{"result":[[{"CODIGO":"1     ","NOME":"PLASTICOS"},{"CODIGO":"2     ","NOME":"VIDROS"},{"CODIGO":"3     ","NOME":"BRINQUEDOS"},{"CODIGO":"4     ","NOME":"IMPORTADOS"},{"CODIGO":"5     ","NOME":"METAIS"},{"CODIGO":"6     ","NOME":"PAPELARIA"},{"CODIGO":"7     ","NOME":"T\u00C9RMICOS"},{"CODIGO":"8     ","NOME":"DIVERSOS"}]]}


Hi.

Bookmark this website: JSONLint - The JSON Validator.

You can paste a JSON String or a Url to a JSON feed and it will not only tell you if the JSON is valid but also nicely format it for you!

Martin.
 
Upvote 0

luiswagnersantos

Member
Licensed User
Longtime User
Hie,

thanks for the help

My code basic4ppc

PHP:
Sub btn_Jason_Click
Dim JSON As JSONParser
Dim Map1 As Map

JSON.Initialize(File.ReadString(File.DirDefaultExternal, "marana.json"))
Map1 = JSON.NextObject
Dim m As Map 'helper map for navigating
Dim MenuItems As List

m = Map1.Get("result")
'm = m.Get("codigo")
'MenuItems = m.Get("codigo")

'For i = 0 To MenuItems.Size - 1
    'm = MenuItems.Get("CODIGO")
    Log(m.Get("NOME"))
'Next



'm = Map1.Get("menu")
'm = m.Get("popup")
'MenuItems = m.Get("menuitem")
'
'For i = 0 To MenuItems.Size - 1
'    m = MenuItems.Get(i)
'    Log(m.Get("value"))
'Next



End Sub

error log

PHP:
java.lang.ClassCastException: java.util.ArrayList
   at lws.json.main._btn_jason_click(main.java:287)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:113)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:101)
   at anywheresoftware.b4a.BA.raiseEvent(BA.java:97)
   at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:49)
   at android.view.View.performClick(View.java:2485)
   at android.view.View$PerformClick.run(View.java:9080)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:123)
   at android.app.ActivityThread.main(ActivityThread.java:3647)
   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.ClassCastException: java.util.ArrayList



Are you getting an error?

Why do you do: Log(m.Get("value")) ??
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
I'm not very good at this but I think it should be:

B4X:
Dim JSON As JSONParser 
Dim Map1 As Map 

JSON.Initialize(File.ReadString(File.DirDefaultExternal, "marana.json")) 
Map1 = JSON.NextObject 
Dim m As Map helper map for navigating 
Dim MenuItems As Map

MenuItems = Map1.Get("result") 

For i = 0 To MenuItems.Size - 1 
    m = MenuItems.Get(i) 
    Log(m.Get("CODIGO")) 
    Log(m.Get("NOME")) 
Next

Also, I think you should attach the debugger so we can see the exact line number going wrong.
 
Upvote 0

luiswagnersantos

Member
Licensed User
Longtime User
This code is giving work:)

I'm reading this reference:
http://www.b4x.com/forum/basic4android-updates-questions/10671-blasted-json-argh.html

Json file of this post is very different from my
Is this the problem?
Running the code below


PHP:
Dim JSON As JSONParser 
Dim Map1 As Map 

JSON.Initialize(File.ReadString(File.DirDefaultExternal, "marana.json")) 
Map1 = JSON.NextObject 
Dim m As Map 'helper Map For navigating 
Dim MenuItems As Map

MenuItems = Map1.Get("result") 

For i = 0 To MenuItems.Size - 1 
    m = MenuItems.Get(i) 
    Log(m.Get("CODIGO")) 
    Log(m.Get("NOME")) 
Next

The error in this line

PHP:
MenuItems = Map1.Get("result")

Error
PHP:
java.lang.ClassCastException: java.util.ArrayList

had tried unsuccessfully this code

:)


I'm not very good at this but I think it should be:

B4X:
Dim JSON As JSONParser 
Dim Map1 As Map 

JSON.Initialize(File.ReadString(File.DirDefaultExternal, "marana.json")) 
Map1 = JSON.NextObject 
Dim m As Map helper map for navigating 
Dim MenuItems As Map

MenuItems = Map1.Get("result") 

For i = 0 To MenuItems.Size - 1 
    m = MenuItems.Get(i) 
    Log(m.Get("CODIGO")) 
    Log(m.Get("NOME")) 
Next

Also, I think you should attach the debugger so we can see the exact line number going wrong.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
This code is giving work:)

I'm reading this reference:
http://www.b4x.com/forum/basic4android-updates-questions/10671-blasted-json-argh.html

Json file of this post is very different from my
Is this the problem?
Yes, you have an array in your JSON in a different position.

Try this:
B4X:
Dim JSON As JSONParser 
Dim Map1 As Map 

JSON.Initialize(File.ReadString(File.DirDefaultExternal, "marana.json")) 
Map1 = JSON.NextObject 
Dim m As Map 'helper Map For navigating 
Dim MenuItems As List

MenuItems = Map1.Get("result") 

For i = 0 To MenuItems.Size - 1 
    m = MenuItems.Get(i) 
    Log(m.Get("CODIGO")) 
    Log(m.Get("NOME")) 
Next

If I was at my PC, I could have tried this myself.
 
Upvote 0

brunoakun

Member
Licensed User
Longtime User
Json response

Hi!
Great, but may be is more corect putting the square brakets in the json response "[]"

JSON.Initialize(File.ReadString(File.DirDefaultExternal, "[" & "marana.json" & "]"))

Saludos
Bruno
 
Upvote 0
Top