将json解析为listmodel

JRi*_*Rii 0 json qml

我部分成功地将 json 解析为我的 qml 应用程序,但只是部分成功。我可以通过 console.log() 很好地输出 countryCode 和 countryName,最终目标仍然是将 json 解析的数据附加到 listmodel。仍然无法输出带有年份和实际数据数据部分的 bigmac_index。

显然已经尝试了不同的解决方案并尝试应用主题的给定答案,但没有成功。希望从工作解决方案中受益:)

json 部分: [ { "countryCode": "fi", "countryName": "Finland", "bigmac_index": [ { "year": "2013", "data": "5.27" }, { "year": " 2012", "data": "4.55" }, { "year": "2011", "data": "5.38" } ] } ]

这是我正在使用的功能:

    function request(url, callback) {
        var xhr = new XMLHttpRequest();

        console.log("xhr.send executed")

        xhr.onreadystatechange = (function()
        {
            console.log("xhr readystate ", xhr.readyState)
            if(xhr.readyState == 4 && xhr.status == 200)
                {
                    console.log("readyState == 4, starting to parse")
                    var parseData = JSON.parse(xhr.responseText);

                    for ( var index in parseData)
                    {
                  console.log("countrycode, ", parseData[index].countryCode)              
                  console.log("countryName, ", parseData[index].countryName)
                  console.log("datakey, ", parseData[index].bigmac_index) 

                  //Attemp to parse to ListModel

                  lmodel.append
                  ({
                          "countryCode" : parseData.countryCode,
                          "countryName" : parseData[index].countryName,
                          "datakey" : parseData[index].bigmac_index
                   })}
                }

            else
            {
                console.log("readyState, ", xhr.readyState)
            }
        }

        );
        xhr.open('GET', url, true);
        xhr.send();
    }
Run Code Online (Sandbox Code Playgroud)

json结构+api数据:http : //blog.inqubu.com/inqstats-open-api-published-to-get-demographic-data

fol*_*bis 6

欢迎来到 SO,@JRii!首先,您应该在提问之前阅读页面。至于你的问题 - 你所做的一切都是对的,但你没有提供你的 QML 代码,所以不可能理解你做错了什么。无论如何,这应该有效:

假设数据是:

[
    {
        "countryCode": "us",
        "countryName": "USA",
        "population": [
            {
                "year": "2014",
                "data": "318857056"
            },
            {
                "year": "2013",
                "data": "316497531"
            },
            {
                "year": "2012",
                "data": "314112078"
            },
            {
                "year": "2011",
                "data": "311721632"
            },
            {
                "year": "2010",
                "data": "309347057"
            }
        ]
    }
]
Run Code Online (Sandbox Code Playgroud)

应该解析和显示数据的 QML 代码:

ListView {
    anchors.fill: parent
    model: ListModel { id: model}
    delegate: Text { text: "[" + year + "]: " + population }
    Component.onCompleted: {
        var xhr = new XMLHttpRequest;
        xhr.open("GET", "http://inqstatsapi.inqubu.com/?api_key=YOURKEYHERE&data=population&countries=us");
        xhr.onreadystatechange = function() {
            if (xhr.readyState === XMLHttpRequest.DONE) {
                var data = JSON.parse(xhr.responseText);
                model.clear();
                var list = data[0]["population"];
                for (var i in list) {
                    model.append({year: list[i]["year"], population: list[i]["data"]});
                }
            }
        }
        xhr.send();
    }
}
Run Code Online (Sandbox Code Playgroud)