Javascript qml JSON递归解析非平面JSON结果集
我正在使用一个带有Javascript qml JSON递归解析非平面JSON结果集,javascript,json,qt,qml,Javascript,Json,Qt,Qml,我正在使用一个带有XMLHttpRequest的简单Web API。使用此QML代码,API将以JSON结果集进行响应 Page1 { Component.onCompleted: { getInformation(xyz) } function getInformation(xyz) { var req = new XMLHttpRequest; var httpString = "https://api.upcitemd
XMLHttpRequest
的简单Web API。使用此QML代码,API将以JSON结果集进行响应
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();
}
}
代码很好,我一直在得到一个结果,但结果如下所示:
thekey: code
the Object: OK
thekey: total
the Object: 1
thekey: offset
the Object: 0
thekey: items
the Object: [[object Object]]
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:
我想得到最后一个对象的详细信息,
对象:[[object object]]
,我知道它是另一个键/值
对JSON集。。我发现了类似的帖子,用ajax
解决了这个问题,但我不知道也不使用ajax
,我正在寻找将QML代码中的最后一个对象转换为另一个数组的选项?在for循环中,你试过检查jsonObject类型吗?然后可以从中打印出内部节点数据
类似于
if (typeof(jsonObject) === "object") {
var innerObject = jsonObject;
for (var innerkey in innerObject ) {
//Do stuff..
}
}
具有转储json对象并递归使用它的函数:
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]);
}
}
}
在你的职能中:
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();
}
upc代码042100005264的输出如下:
thekey: code
the Object: OK
thekey: total
the Object: 1
thekey: offset
the Object: 0
thekey: items
the Object: [[object Object]]
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:
问题不在解析中,
JSON.parse()
应该可以正常工作,问题是qt没有在控制台中正确显示js对象,并且您没有递归迭代。@dtech,是的,我当然知道解析工作正常,但我不知道如何递归迭代JSON结果集。我将尝试p-a-o-l-o函数,我想我应该这样做。什么是“缩进”和“索引”。。任意变量?indent
是一个在每次递归时递增的整数,ind
是一个放置在每行开头的indent*2
空格字符串。如果你不在乎缩进,你可以完全摆脱它们。顺便说一下,我编辑了这个函数来显示objects键。是的,我会根据需要得到它。