Skip to first unread message

Em ulien

unread,
Jun 5, 2019, 1:43:25 PM6/5/19
to MIT App Inventor Forum
hi

i want to parse this object:

{"blues":[{"id":"1","codeb":7000001,"prenom":"je","nom":"test","piece":"v","datecreation":"1989-12-11T23:00:00.000Z","datesortie":"1990-12-12T23:00:00.000Z","dateretour":"1990-12-13T23:00:00.000Z","etat":"g"},{"id":"1","codeb":7000002,"prenom":"je","nom":"test","piece":"v","datecreation":"1989-12-12T23:00:00.000Z","datesortie":"1990-12-13T23:00:00.000Z","dateretour":"1990-12-14T23:00:00.000Z","etat":"g"},{"id":"1","codeb":7000003,"prenom":"je","nom":"test","piece":"v","datecreation":"1989-12-13T23:00:00.000Z","datesortie":"1990-12-14T23:00:00.000Z","dateretour":"1990-12-15T23:00:00.000Z","etat":"g"},{"id":"1","codeb":7000004,"prenom":"je","nom":"test","piece":"p","datecreation":"1989-12-14T23:00:00.000Z","datesortie":"1990-12-15T23:00:00.000Z","dateretour":"1990-12-16T23:00:00.000Z","etat":"g"},{"id":"1","codeb":7000022,"prenom":"je","nom":"test","piece":"p","datecreation":"1989-12-15T23:00:00.000Z","datesortie":"1990-12-16T23:00:00.000Z","dateretour":"1990-12-17T23:00:00.000Z","etat":"g"},{"id":"1","codeb":7000005,"prenom":"je","nom":"test","piece":"p","datecreation":"1989-12-16T23:00:00.000Z","datesortie":"1990-12-17T23:00:00.000Z","dateretour":"1990-12-18T23:00:00.000Z","etat":"g"},{"id":"1","codeb":7000006,"prenom":"je","nom":"test","piece":"p","datecreation":"1989-12-17T23:00:00.000Z","datesortie":"1990-12-18T23:00:00.000Z","dateretour":"1990-12-19T23:00:00.000Z","etat":"g"}]}

before parsing i just want to know how many objects it has (according to me :7)

but when i try this it founds only one i don't understand




help.pngli












regards

 

Andrés Martín

unread,
Jun 5, 2019, 2:10:35 PM6/5/19
to MIT App Inventor Forum
Hola

Quita el Length of list list

Y pon a Label1.text get global us 

Vas a ver tu mismo el resultado

Y es que el resultado es "blues":(El elemento siguiente que consta de 7)

Em ulien

unread,
Jun 5, 2019, 3:07:31 PM6/5/19
to mitappinv...@googlegroups.com
hola

thanks for your answer

i did what you said i removed length of list 


i can  see the response but i want the app count and give me the result what i can't get so far 

regards

Andrés Martín

unread,
Jun 5, 2019, 3:44:05 PM6/5/19
to MIT App Inventor Forum
Ok lo miro y si doy con la solución te la cuento. Yo no tengo mucha experiencia en tu caso pero lo intentaré, vale.

Saludos

Andrés Martín

unread,
Jun 5, 2019, 3:55:58 PM6/5/19
to MIT App Inventor Forum
Oye y por que no lo tratas como una cadena de caracteres. 

Sabes que el json es una Clave:Valor y despues una "," osea Clave:Valor,Clave:Valor y que la Clave puede acceder a una lista de valores con {1,2,3},{1,2,3} analiza la cadena y pon tu las reglas.

Esto es lo que yo haría:

Eliminaría el "blues":[ y buscaría el cierre para eliminarlo ]

me quedaría con {},{}....7 veces

Pondría un bucle While do con un contador y añadiría 1:{}, 2:{}..... aunque así ya lo cuentas si te fijas.

Oye tienes que hacerlo tú o solo tienes un elemento en tu json, lo siento no veo otra solución

Evan Patton

unread,
Jun 5, 2019, 5:26:38 PM6/5/19
to MIT App Inventor Forum
First, do not call "make a list" with the output of Web1.JsonTextDecode. If the JSON is an object structure, it will be returned as a lists of lists structure where the first element of each list is the key and the second element is the value. In this case, it will be a list of length 1, where the first element of the singular list will be "blues" and the second will be a list containing the elements of the array. You can use the "lookup in pairs" block to retrieve the contents of this key and then call "length of list" on that:

blocks(1).png

Regards,
Evan

Em ulien

unread,
Jun 6, 2019, 6:52:48 AM6/6/19
to mitappinv...@googlegroups.com
hi Evan

thanks for you proposal

 i can get what i want : number of items of "blue"

now i have to find a way to browse "blues" and get any data from blues


i don't forget Andrès

Ghica

unread,
Jun 6, 2019, 10:27:10 AM6/6/19
to MIT App Inventor Forum
Your json has a bit of extra stuf. If you do this:

Snap23.png

Then you will see that label3 contains 7.
So, when you browse that list, you will get all your blues.
Cheers, Ghica.


Andrés Martín

unread,
Jun 6, 2019, 12:45:59 PM6/6/19
to MIT App Inventor Forum
Acabo de ver la conversación tuya. 

No te preocupes estoy al acecho. Y sigo intentando te ayudar de la mejor forma. Pero veo que Ghica y Evan lo hacen bien. Una nota con respecto a Evan. Se que el lo sabe, Yo no pondría el texto blues, yo pondría directamente get global us

De esta manera el código es mas genérico.

Evan lo hago con todo el respeto que tu te mereces, no es una critica es solo mas ayuda.

Saludos a todos.

Andrés Martín

unread,
Jun 6, 2019, 1:47:51 PM6/6/19
to MIT App Inventor Forum
Me ha encantado Ghica :)

Tu si que vales.

Evan Patton

unread,
Jun 7, 2019, 4:50:34 PM6/7/19
to MIT App Inventor Forum
Andres,

I am not sure that I understand you properly from the Google Translate text about your proposed approach being more generic. Using any approach other than "lookup in pairs" is naturally more brittle because if the data structure changes, there's no guarantee from the App Inventor side that we will return a list in the same order. For example, consider that the web endpoint returning the JSON data adds a new field:

{
  "reds": [...],
  "blues": [...]
}

JSON dictionaries keys are not ordered. It is equally valid that ((reds (...)) (blues (...)) is returned by Web1.JsonTextDecode as ((blues (...)) (reds (...))). "lookup in pairs" performs the actual key lookup in the list, and so it's guaranteed to always find the corresponding value (if it exists), regardless of the number and order of keys. If you get the first item in the list, you may end up with the list of "reds" not "blues". Similarly, you won't know whether the curly braces you are counting are in one list or the other.

Regards,
Evan

Andrés Martín

unread,
Jun 8, 2019, 4:04:27 AM6/8/19
to MIT App Inventor Forum
Hola Evan :)

Perdón por el español. Se inglés pero como no lo domino prefiero hacerlo en español. 

Si, tu tienes razón. Yo pensaba que se lo estabas explicando como un profesor. Pero bueno no importa. Ahora veo que querías asegurarte de que encontrara la Clave correcta = blues.

Yo solo quería hacer que sea mas generalizado el código.

Bueno no tiene ninguna importancia, era hablar por hablar. Tu tienes razón.

Saludos, joven.

Em ulien

unread,
Jun 9, 2019, 6:38:48 AM6/9/19
to mitappinv...@googlegroups.com
hi evan 

thanks for your answer

i knew one time to extract any item of the list i changed blocks and i forgot it

it's true i'm not a master of jason and app inventor

can you tell me what blocs i have to use if i want to extract codeb of the first item of the list 

i go on searching

regards

it's ok i found it



i wonder i have to put  codeb, id, name  in label1 t have to extract the 3 items and after put them in label1  is there a faster/ easy way to do it ?

regards



help2.jpg













Reply all
Reply to author
Forward
0 new messages