Reputation: 4602
I am consuming a simple Web API with XMLHttpRequest
using this QML code, the API responds with a JSON result set.
Page1 {
Component.onCompleted: {
getInformation(xyz)
}
function getInformation(xyz) {
var req = new XMLHttpRequest;
var httpString = "https://api.upcitemdb.com/prod/trial/lookup?upc="
httpString += xyz
req.open("GET", httpString);
req.onreadystatechange = function() {
var status = req.readyState;
if (status === XMLHttpRequest.DONE) {
var objectArray = JSON.parse(req.responseText);
if (objectArray.errors !== undefined)
console.log("Error fetching barcode: " + objectArray.errors[0].message)
else {
for (var key in objectArray) {
var jsonObject = objectArray[key];
console.log("thekey:" , key)
console.log("the Object:", jsonObject)
}
}
}
}
req.send();
}
}
The code is fine and I am getting a result all the time, but the result looks like this:
thekey: code
the Object: OK
thekey: total
the Object: 1
thekey: offset
the Object: 0
thekey: items
the Object: [[object Object]]
and I want to get the details of the last object the Object: [[object Object]]
which I know is another key/value
pair JSON set .. I found similar posts addressing the issue with ajax
but I don't know nor I am using ajax
, I am seeking options to convert last object into another array in my QML code?
Upvotes: 2
Views: 1402
Reputation: 10077
Have a function to dump a json object and use it recursively:
function dumpJSONObject(jsonObject, indent) {
var ind = new Array(indent * 2).join( ' ' );
for (var key in jsonObject) {
if(typeof(jsonObject[key]) == 'object') {
console.log( ind + key + ": ");
dumpJSONObject(jsonObject[key], indent + 1);
} else {
console.log( ind + key + ": " + jsonObject[key]);
}
}
}
In your function:
function getInformation() {
var req = new XMLHttpRequest;
var httpString = "https://api.upcitemdb.com/prod/trial/lookup?upc="
httpString += "042100005264"
req.open("GET", httpString);
req.onreadystatechange = function() {
var status = req.readyState;
if (status === XMLHttpRequest.DONE) {
var objectArray = JSON.parse(req.responseText);
if (objectArray.errors !== undefined)
console.log("Error fetching barcode: " + objectArray.errors[0].message)
else {
dumpJSONObject( objectArray, 0 ); //here
}
}
}
req.send();
}
The output for upc code 042100005264 is like this:
qml: code: OK
qml: total: 1
qml: offset: 0
qml: items:
qml: 0:
qml: ean: 0042100005264
qml: title: Dewalt Bostitch 12 Volt 12v Max Lithium Ion Cordless Battery Pack
qml: description:
qml: upc: 042100005264
qml: elid: 391768274284
qml: brand: Soft Surroundings
qml: model:
qml: color: Black
qml: size:
qml: dimension:
qml: weight:
qml: lowest_recorded_price: 4.99
qml: highest_recorded_price: 95
qml: images:
qml: offers:
Upvotes: 3
Reputation: 404
Within the for loop have you tried checking jsonObject type? From this you can then print out inner node data..
Something like..
if (typeof(jsonObject) === "object") {
var innerObject = jsonObject;
for (var innerkey in innerObject ) {
//Do stuff..
}
}
Upvotes: 1