Android Question Parse JSON

AlpVir

Well-Known Member
Licensed User
Longtime User
In the post #33 https://www.b4x.com/android/forum/threads/android-json-tutorial.6923/page-2
the user "sioconcept" wrote
Is there a limit size of the JSON string?
and Erel answered
There is no hard limit.

However I tried to parse the 18 MB "c.json" file and got the error
java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack available
This is the simple code
B4X:
Dim List1 As List
    Dim JSON As JSONParser
    JSON.Initialize(File.ReadString(File.DirAssets, "c.json"))
    List1.Initialize
    List1 = JSON.NextArray
    Dim M As Map
    M = List1.Get(0)
    Dim loc As Map
    loc = M.Get("id")
    For i = 0 To 50        ' loc.Size - 1
        Dim Longitude     As Double
        Dim Latitude     As Double
        Dim name         As String
        Longitude = loc.Get ("lon")
        Latitude = loc.Get ("lat")
        name = loc.Get ("name")
        Log(name & "  " & Latitude & "  " & Longitude)
    Next

and this is a very very small part of the "c.json" file
{
"id": 3177841,
"coord": {
"lon": 9.66826,
"lat": 45.358768
},
"country": "IT",
"geoname": {
"cl": "P",
"code": "PPLA3",
"parent": 6540640
},
"name": "Crema",
"stat": {
"level": 1.0,
"population": 32981
},
"stations": [
{
"id": 5794,
"dist": 34,
"kf": 1
},
{
"id": 5800,
"dist": 31,
"kf": 1
},
{
"id": 5820,
"dist": 40,
"kf": 1
},
{
"id": 8977,
"dist": 0,
"kf": 1
},
{
"id": 9552,
"dist": 13,
"kf": 1
},
{
"id": 9798,
"dist": 34,
"kf": 1
},
{
"id": 10222,
"dist": 39,
"kf": 1
},
{
"id": 10368,
"dist": 39,
"kf": 1
},
{
"id": 10799,
"dist": 44,
"kf": 1
},
{
"id": 28814,
"dist": 42,
"kf": 1
},
{
"id": 29222,
"dist": 0,
"kf": 1
},
{
"id": 32018,
"dist": 18,
"kf": 1
},
{
"id": 32561,
"dist": 39,
"kf": 1
},
{
"id": 34103,
"dist": 43,
"kf": 1
},
{
"id": 35260,
"dist": 15,
"kf": 1
},
{
"id": 35663,
"dist": 39,
"kf": 1
}
],
"zoom": 10
},
{
"id": 3177924,
"coord": {
"lon": 8.18172,
"lat": 45.57386
},
"country": "IT",
"geoname": {
"cl": "P",
"code": "PPLA3",
"parent": 6540883
},
"langs": [
{
"de": "Cossato"
},
{
"en": "Cossato"
},
{
"eo": "Cossato"
},
{
"fr": "Cossato"
},
{
"it": "Cossato"
},
{
"link": "http://en.wikipedia.org/wiki/Cossato"
},
{
"nap": "Cossato"
},
{
"nl": "Cossato"
},
{
"pl": "Cossato"
},
{
"pt": "Cossato"
}
],
"name": "Cossato",
"stat": {
"level": 1.0,
"population": 15266
},
"stations": [
{
"id": 5792,
"dist": 43,
"kf": 1
},
{
"id": 30106,
"dist": 47,
"kf": 1
},
{
"id": 30420,
"dist": 48,
"kf": 1
},
{
"id": 32877,
"dist": 48,
"kf": 1
},
{
"id": 33359,
"dist": 36,
"kf": 1
},
{
"id": 33573,
"dist": 47,
"kf": 1
},
{
"id": 34361,
"dist": 27,
"kf": 1
},
{
"id": 34492,
"dist": 46,
"kf": 1
},
{
"id": 35332,
"dist": 32,
"kf": 1
},
{
"id": 36136,
"dist": 35,
"kf": 1
}
],
"zoom": 11
},

How do you explain all this?
Thanks for the attention !
 

Peter Simpson

Expert
Licensed User
Longtime User
I believe that this error is to do with the java heap memory capacity setting and nothing to do with JSON or the list.

You need to allocate more memory to java by setting your Windows enviroment variables for Xmx Xms. If you do a google search for java Xmx Xms heap size, you will find clear instructions on how to increase it to let say 4GB which is what mine is set to.

Also search for Xincgc, that may help you too.

That's my guess anyway, but I could be wrong...

Enjoy...
 
Last edited:
Upvote 0

AlpVir

Well-Known Member
Licensed User
Longtime User
Thank you. My final goal is to turn these json files (even 28 MB !) into db sqlite. I will try to transform them with other software tools.
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
By the way you can easily tell if you have implemented Xmx and Xms correctly through Windows, as every time you run any B4J project even a blank project, you will see the following line appear in the logs when you stop the project.

Picked up _JAVA_OPTIONS: -Xmx2g -Xms2g

Enjoy...
 
Last edited:
Upvote 0
Top