Ler subitem de um JSON
23/01/2020
0
Olá. Preciso fazer a leitura dos subitens de um JSON. Os items consigo buscar perfeitamento conforme o código abaixo. O que não consegui foi a partir de cada item, buscar as informações dos "subItems". Alguém pode me ajudar?
{
"items":[
{
"name":"Pastel de queijo",
"quantity":2,
"price":5.0,
"subItemsPrice":0,
"totalPrice":5.0,
"discount":0.0,
"addition":0.0,
"externalCode":"2222",
"subItems":[
{
"name":"Alface",
"quantity":1,
"price":1.5,
"totalPrice":1.5,
"discount":0.0,
"addition":0.0,
"externalCode":"13"
},
{
"name":"Bacon",
"quantity":1,
"price":3.0,
"totalPrice":3.0,
"discount":0.0,
"addition":0.0,
"externalCode":"11"
}
],
"observations":"SEM MAIONESE"
},
{
"name":"X-burguer bacon",
"quantity":1,
"price":8.0,
"subItemsPrice":4.5,
"totalPrice":12.5,
"discount":0.0,
"addition":0.0,
"externalCode":"7",
"subItems":[
{
"name":"Alface",
"quantity":1,
"price":1.5,
"totalPrice":1.5,
"discount":0.0,
"addition":0.0,
"externalCode":"13"
},
{
"name":"Bacon",
"quantity":1,
"price":3.0,
"totalPrice":3.0,
"discount":0.0,
"addition":0.0,
"externalCode":"11"
}
],
"observations":"SEM MAIONESE"
}
]
}
var jsonObj, jSubObj: TJSONObject; ja: TJSONArray; jv: TJSONValue; i: Integer; begin jsonObj := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(Memo1.Text), 0) as TJSONObject; jv := jsonObj.Get(''items'').JsonValue; ja := jv as TJSONArray; Memo2.Clear; for i := 0 to ja.Size - 1 do begin jSubObj := (ja.Get(i) as TJSONObject); Memo2.Lines.Add(jSubObj.GetValue<string>(''name'')); Memo2.Lines.Add(jSubObj.GetValue<string>(''quantity'')); Memo2.Lines.Add(jSubObj.GetValue<string>(''price'')); Memo2.Lines.Add(jSubObj.GetValue<string>(''subItemsPrice'')); Memo2.Lines.Add(jSubObj.GetValue<string>(''totalPrice'')); Memo2.Lines.Add(jSubObj.GetValue<string>(''discount'')); Memo2.Lines.Add(jSubObj.GetValue<string>(''addition'')); Memo2.Lines.Add(jSubObj.GetValue<string>(''externalCode'')); Memo2.Lines.Add(jSubObj.GetValue<string>(''observations'')); end; end;
{
"items":[
{
"name":"Pastel de queijo",
"quantity":2,
"price":5.0,
"subItemsPrice":0,
"totalPrice":5.0,
"discount":0.0,
"addition":0.0,
"externalCode":"2222",
"subItems":[
{
"name":"Alface",
"quantity":1,
"price":1.5,
"totalPrice":1.5,
"discount":0.0,
"addition":0.0,
"externalCode":"13"
},
{
"name":"Bacon",
"quantity":1,
"price":3.0,
"totalPrice":3.0,
"discount":0.0,
"addition":0.0,
"externalCode":"11"
}
],
"observations":"SEM MAIONESE"
},
{
"name":"X-burguer bacon",
"quantity":1,
"price":8.0,
"subItemsPrice":4.5,
"totalPrice":12.5,
"discount":0.0,
"addition":0.0,
"externalCode":"7",
"subItems":[
{
"name":"Alface",
"quantity":1,
"price":1.5,
"totalPrice":1.5,
"discount":0.0,
"addition":0.0,
"externalCode":"13"
},
{
"name":"Bacon",
"quantity":1,
"price":3.0,
"totalPrice":3.0,
"discount":0.0,
"addition":0.0,
"externalCode":"11"
}
],
"observations":"SEM MAIONESE"
}
]
}
Jhon
Curtir tópico
+ 0
Responder
Clique aqui para fazer login e interagir na Comunidade :)