iOS Question JSON differences B4i B4A

manolitoGnet

Member
Licensed User
Longtime User
Hi,

First of all, Why iJSON lacks .NextValue? (I'm using it in B4A...)

And, Why a string that is perfectly parsed in B4A fails with "Garbage at the end" in b4i?

Thx

Román
 

manolitoGnet

Member
Licensed User
Longtime User
Second first:

"18"{"name":"Pep"}{"name":"Pep"}{"name":"Pep"}{"name":"Pep"}... ...{"name":"Pep"}

The "header" contains de number of objects. YES IT IS BAD FORMED JSON, but in B4A JSON 1.10 this code works:

B4X:
Sub Parser_JSON
  Dim JSON As JSONParser
  Dim Map1 As Map
  Dim filas As Int

  Map1.Initialize
  If HTML1.Contains("name") Then
    JSON.Initialize(HTML1)

    filas = JSON.Nextvalue '<--- the answer to the first question
    For i = 0 To filas -1
      Map1 = JSON.NextObject    
      listaValores(i).name = Map1.Get("name")
    Next

but in B4i the parser knows that is a malformed JSON and trows an error...

Don`t worry, I have control over the source and I can correct the JSON string.

Curious. Isn't it?

Thanks!
 
Upvote 0

manolitoGnet

Member
Licensed User
Longtime User
SOLVED

The correct JSON string and correct code...

[{"name":"Pep"},{"name":"Pep"},{"name":"Pep"},{"name":"Pep"},... ...{"name":"Pep"}]

B4X:
Sub Parser_JSON
  Dim JSON As JSONParser
  Dim Map1 As Map
  Dim list1 As List
  Dim i As Int

  Map1.Initialize
  If HTML1.Contains("name") Then '<------ optional or more complex check
    JSON.Initialize(HTML1)
    list1 = JSON.NextObject

    For i = 0 To list1.Size -1 '<------------ no previous number of object needed
      Map1 = list1.Get(i)
      listaValores(i).name = Map1.Get("name")
    Next

Hope will be useful for someone.

(all of this because I'm porting an App from B4A to B4i and I'm reusing complete Subs with nothing or minimal changes -UI excluded-)
 
Upvote 0
Top