2017-03-20 21 views
0

私はqml applにjsonを構文解析することで部分的に成功していますが、部分的にしかありません。私はcountryCodeとcountryNameをconsole.log()経由で出力することができますが、endターゲットはjsonの解析データをlistmodelに追加します。 bigmac_indexには年と実際のデータのデータ部分を出力できません。listmodelにjsonを構文解析する

明らかに別の解決策を試してみて、被験者の回答を適用しようとしましたが、失敗しました。ワーキング溶液から感謝:)

JSON一部: [ { "国番号": "Fiの"、 "COUNTRYNAME": "フィンランド"、 "bigmac_index":[ { "年": " 2013" 、 "データ": "5.27" }、 { "年": "2012"、 "データ": "4.55" }、 { "年": "2011"、 「データ":" 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(); 
    } 

JSON構造+ APIデータ: http://blog.inqubu.com/inqstats-open-api-published-to-get-demographic-data

答えて

0

へようこそSO、@JRii! まず、質問をする前にthisページをお読みください。 あなたの質問は正しいですが、あなたは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" 
      } 
     ] 
    } 
] 

データを解析して表示する必要があります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(); 
    } 
} 
+0

Thxを、あなたの応答に基づいて調整コード。 – JRii

+0

この作業ができました。問題の解決方法を覚える必要があります:) – JRii