如何使用qml访问blackberry 10中的嵌套数组JSON文件

时间:2014-09-19 10:26:00

标签: json blackberry qml blackberry-10

我有以下json文件我试图使用Qml从上面的嵌套数组Json文件中访问时间,百分比和数量。我无法获得这些数据。我希望这些数据填写一个有3列的表。请告诉我怎么做。



sample.json:
{
  "rates": [
    {
      "time": "3 Months",
      "interest": [
        {
          "percent": "0.01",
          "amount": "2500"
        },
        {
          "percent": "0.02",
          "amount": "5000"
        },
        {
          "percent": "0.09",
          "amount": "10000"
        }
      ]
    },
    {
      "time": "6 Months",
      "interest": [
        {
          "percent": "0.10",
          "amount": "2500"
        },
        {
          "percent": "0.11",
          "amount": "5000"
        },
        {
          "percent": "0.12",
          "amount": "10000"
        }
      ]
    },
    {
      "time": "1 Year",
      "interest": [
        {
          "percent": "0.11",
          "amount": "2500"
        },
        {
          "percent": "0.12",
          "amount": "5000"
        },
        {
          "percent": "0.14",
          "amount": "10000"
        }
      ]
    }
  ]
}



     

1 个答案:

答案 0 :(得分:0)

这是您加载和获取数据的方式。如何进入一个表不应该是从这里努力

import bb.cascades 1.2
import bb.data 1.0
Page {
    Container {

    }
    attachedObjects: [
        DataSource {
            id: dataSource
            source: "sample.json"
            type: DataSourceType.Json
            onDataLoaded: {
                console.log(data.rates);
                console.log(JSON.stringify(data));
                for(var i = 0; i < data.rates.length; i++){
                    console.log(JSON.stringify(data.rates[i]));
                    console.log("Time: " + data.rates[i].time);

                    for(var j =0; j < data.rates[i].interest.length; j++)
                    {
                        console.log(JSON.stringify(data.rates[i].interest[j]));
                        console.log("Percent: " + data.rates[i].interest[j].percent);
                        console.log("Amount: " + data.rates[i].interest[j].amount);
                    }
                }
            }
        }
    ]
    onCreationCompleted: {
        dataSource.load();
    }
}