Hi. I am new in ionic-2. I need to get json data using http request and store the data in array. below is the sample json data.
{
"data": [
{
"id": 4,
"date": "2016-11-17",
"product": "Lillium",
"product_id": 3,
"product_type": "Flower",
"quantity_produced": 200,
"created": 1495591050
},
{
"id": 5,
"date": "2016-12-06",
"product": "Gerbera",
"product_id": 1,
"product_type": "Flower",
"quantity_produced": 300,
"created": 1495591067
}
]
}
from the above json data, i need to store only “quantity_produced” values in array.
for eg: [ 200, 300, 500, 110, 220 ]. please suggest any solution. thank you.
1 Like
fishi
July 18, 2017, 6:08am
2
I’m sorry with your post I have no idea where your issue is. If you don’t know how the get data via http
follow the guide from Joshua
Although some mobile applications are completely self contained (like calculators, soundboards, flashlight apps), many applications rely on pulling in data from an external source to function. Face…
if you don’t know how to store data, then please read first the Angular2 guides.
https://angular.io/
hope it helps.
I think a quick & simple way to do it would be :
var json;
this.http.get('link/to/your/api').map(res => res.json()).subscribe(data => {
json = data;
});
and then parse it in this way
var qp = []
for (var i of json.data) {
qp.push(i.quantity_produced);
}
Here is a link to the parse script
Hope this solves your problem.
4 Likes
thank you. your parse data technique was usefull. @saidatta123
1 Like
Thanks a lot bro, you saved my day.
skydevelopers
I don’t see what everybody else apparently does with regard to the post marked as the solution here. It has poorly-delineated spheres of control and a needless loop that could cause lots of unnecessary allocations. What I would do instead:
interface Product {
id: number;
date: string;
// &c
}
interface ProductsResponse {
data: Product[];
}
class Provider {
constructor(private _http: HttpClient) {}
allProducts(): Observable<Product[]> {
return this._http.get<ProductsResponse>(url).pipe(
map(prsp => prsp.data));
onlyQuantities(): Observable<number[]> {
return this.allProducts().pipe(
map(prods => prods.map(prod => prod["quantity_produced"])));
}
}
Sorry rapropos, by the OP request an array of quantity_produced
So the pipe needs a mergeAll and a map to quantity_produced
1 Like
Heh. I’m skeptical about that, because it would be hard to do anything with those numbers devoid of context, but good catch. Added a onlyQuantities()
method that delivers that.
1 Like
Yup. But lets see if the OP person will go for the more advanced and future proof solution in RXjs
albincs
February 14, 2019, 6:11pm
10
saidatta123:
map
.map error. how to solve??
Feri98
May 15, 2019, 8:23pm
11
Hy can you help me because this urgent…
I have problem with json array dimention
This is my json : http://202.179.137.23:8077/boaapproval/?mod=Menu
But i the can,t get data json…