Android Question JsonTree tool incomplete parsing

Hamied Abou Hulaikah

Well-Known Member
Licensed User
Longtime User
JsonTree is great tool & save us a lot of time.
in this example, parsing is not complete!
B4X:
[
{car:"Toyota",model:"Prado"},
{year:2020,price:20000},
{year:2019,price:17000},
{year:2015,price:12000}
]
We got this:
B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As List = parser.NextArray
For Each colroot As Map In root
    Dim car As String = colroot.Get("car")
    Dim model As String = colroot.Get("model")
Next
where is the remaining of Json input parsing? "year,price"
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
While it is a valid json, it is designed badly. Arrays are expected to hold items of the same type.

Better design:
B4X:
{
   car:"Toyota",
   model:"Prado",
   prices: [
     {year:2020,price:20000},
     {year:2019,price:17000},
     {year:2015,price:12000}
   ]
}

JsonTree cannot treat different items in an array differently. Parsing this json string requires all kinds of assumptions that might not be true.
 
Upvote 0
Top