App inventor 2 en español
Cómo programar los teléfonos móviles con Android
mediante App inventor 2 - Juan Antonio Villalpando
-- Tutorial de iniciación de App Inventor 2 en español --
Volver al índice del tutorial
____________________________
299G.- Extensión. Decodifica Json. JsonPath. Convertir de Json a Xml y de Xml a Json.
com.KIO4_DecoJson.aix
- Mediante esta extensión podemos obtener los valores de un JSON, escribiendo su ruta.
- Además también podemos convertir cadenas Xml a Json y viceversa.
- https://www.json.org/json-es.html
- En JSON podemos encontrar:
- Objeto, los elementos van en {llave}, por ejemplo:
{"edad":25, "nombre":"Juan Antonio ", "valor":true}
- Array, los elementos van en [corchete], por ejemplo:
["24", "Pedro Luis", false]
- En el Array los elementos están númerados: store.book[1]
1.- Ejemplos de JSON: https://json-schema.org/learn/miscellaneous-examples.html
2.- Ejemplos de JSON: https://opensource.adobe.com/Spry/samples/data_region/JSONDataSetSample.html
3.- Validador de JSON: https://jsonformatter.curiousconcept.com/
4.- Validador de JSONPATH: https://jsonpath.com/
- Para convertir cadena de XML a JSON y de JSON a XML he utilizado la librería:
https://github.com/smart-fun/XmlToJson
- Para obtener los valores por path he utilizado la librería:
https://github.com/json-path/JsonPath
- Podemos obtener el valor o lista de un elemento escribiendo su ruta:
$.store.bicycle.color
- En esta extensión no es necesario poner el $.
store.bicycle.color
- Para realizar las pruebas utilizaremos el ejemplo del autor:
|
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
|
___________________________
0.- Bloque de la extensión.
___________________________
1.- Decodifica un texto en JSON.
p299G_DecodificaJson.aia
- store.bicycle.color
___________________________
- Diseño.
___________________________
- Bloques.
___________________________
2.- Bloque Pretty. Valida Json y lo muestra de forma más legible.
p299G_DecodificaJson_pretty.aia
- Valida el texto en Json, en caso que no cumpla obtendremos el mensaje "Malformed Json"
- Muestra de forma legible el string Json.
___________________________
- Diseño.
- Ejemplo de Json: https://petstore.swagger.io/v2/swagger.json
___________________________
- Bloques.
______________________________________________
3.- Obtiene una lista de temperaturas.
p299G_DecodificaJson_temper.aia
___________________________
- Diseño.
__________________________
- Bloques.
______________________________________________
4.- Convertir una cadena en XML a JSON y luego de JSON a XML.
p299G_DecodificaJson_xml_json.aia
- Vamos a convertir este texto XML a JSON y luego convertiremos el texto JSON obtenido, a XML.
|
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>Título del RSS</title>
<description>Descripción del RSS</description>
<link>http://www.kio4.com/appinventor</link>
<lastBuildDate>Mon, 06 Jan 2022 00:01:00 </lastBuildDate>
<pubDate>Mon, 06 Jan 2022 16:20:00 </pubDate>
<ttl>1800</ttl>
<item>
<title>Entrada dentro del RSS</title>
<description>Descripción de la entrada</description>
<link>http://www.kio4.com/raspberry</link>
<guid>clave única</guid>
<pubDate>Mon, 06 Jan 2022 17:20:00 </pubDate>
</item>
</channel>
</rss>
|
___________________________
- Diseño.
___________________________
- Bloques.
______________________________________________
5.- Obtener nombre.
p299G_DecodificaJson_nombre.aia
- Tenemos este json, debemos obtener los "name" cuyo tags contenga "fat".
- Obtendremos: "Fargus","Bubus"
animales.json |
{
"animals": [
{
"spec": "dog",
"name": "Fargus",
"desc": "agressive",
"age": 12,
"tags": [
"home",
"outside",
"fat"
]
},
{
"spec": "cat",
"name": "Bubus",
"desc": "gentle",
"age": 7,
"tags": [
"home",
"fat"
]
},
{
"spec": "cat",
"name": "Lusia",
"desc": "gentle",
"age": 6,
"tags": [
"home",
"outside"
]
}
]
}
|
___________________________
- Bloques.
.animals[?(@.tags[-1]=='fat')].name
___________________________
6.- Poner un valor. SetValue.
- Mediante el bloque SetValue podemos establecer un valor.
- En este ejemplo he cambiado el author "Herman Melville" por "Juan Antonio".
|
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
}
|
- Este es el book[2]
- En path he puesto: .book[2].author
|
{
"category": "fiction",
"author": "Juan Antonio",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
}
|
______________________________________________
6.- Otro ejemplo.
p299G_DecodificaJson_ListView.aia
raw.json
https://community.appinventor.mit.edu/t/help-in-long-and-complicated-json-how-do-i-get-the-value-which-i-want/74538/4
___________________________
|