【问题标题】:Looping through a JSON File - android循环通过 JSON 文件 - android
【发布时间】:2015-01-19 11:07:09
【问题描述】:

我目前正在制作一个应用程序来订购餐点,作为一项小任务的一部分。我有一个 JSON 文件,当您在微调器上选择您想要的餐点时会读取该文件。但我需要一个可以遍历 JSON 文件的 for 循环。

JSON 文件

[
  {
    Name: "No Meal Selected",
    SandPrice: "0.00",
    RegPrice: "0.00"
  },
  {
    Name: "OFFER (See Notes)",
    SandPrice: "3.79",
    RegPrice: "5.29"
  },
  {
    Name: "Whopper",
    SandPrice: "3.79",
    RegPrice: "5.29"
  },
  {
    Name: "Double Whopper",
    SandPrice: "4.79",
    RegPrice: "6.29"
  },
  {
    Name: "Whopper Junior",
    SandPrice: "2.19",
    RegPrice: "3.69"
  },
  {
    Name: "Whopper Bacon And Cheese",
    SandPrice: "4.59",
    RegPrice: "6.09"
  },
  {
    Name: "Angus XT Steakhouse",
    SandPrice: "5.59",
    RegPrice: "7.09 "
  },
  {
    Name: "Angus XT Classic",
    SandPrice: "5.09",
    RegPrice: "6.59 "
  },
  {
    Name: "Angus XT Smoked Bacon And Cheddar",
    SandPrice: "5.59",
    RegPrice: "7.09 "
  },
  {
    Name: "Angus Steakhouse",
    SandPrice: "4.99",
    RegPrice: "6.49 "
  },
  {
    Name: "Angus Classic",
    SandPrice: "4.49",
    RegPrice: "5.99 "
  },
  {
    Name: "Angus Smoked Bacon And Cheddar",
    SandPrice: "4.99",
    RegPrice: "6.49 "
  },
  {
    Name: "Angus Steakhouse Double",
    SandPrice: "5.99",
    RegPrice: "7.49 "
  },
  {
    Name: "Angus Classic Double",
    SandPrice: "5.49",
    RegPrice: "6.99 "
  },
  {
    Name: "Angus Smoked Bacon And Cheddar Double",
    SandPrice: "5.99",
    RegPrice: "7.49 "
  }
]


^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
JSON i need to loop through

如果有人可以请帮助我,那将非常有帮助,

谢谢。

【问题讨论】:

  • 发布您的示例 json
  • [ {Name:"No Meal Selected",SandPrice:"0.00", RegPrice:"0.00" }, {Name:"OFFER (See Notes)",SandPrice:"3.79", RegPrice: "5.29" }, {Name:"Whopper",SandPrice:"3.79", RegPrice:"5.29" }, {Name:"Double Whopper",SandPrice:"4.79", RegPrice:"6.29"}, {Name:" Whopper Junior",SandPrice:"2.19", RegPrice:"3.69"}, {Name:"Whopper Bacon And Cheese",SandPrice:"4.59", RegPrice:"6.09"}, ] 这就是我需要的 for 循环循环遍历
  • 更新您的问题
  • 你是从 webservice 得到这个?
  • 没有来自我保存在 android studio 中的文件

标签: android json for-loop


【解决方案1】:
JSONArray items = jsonObj.getJSONArray("nameOfArray");

for(int i = 0; i < items.length(); i++)
{
    JSONObject item = items.getJSONObject(i);
    //...
}

【讨论】:

    【解决方案2】:

    您提供的 JSON 格式不正确。 首先,JSON 文件应该以对象开头,而不是数组:

    {"Array":[*contents of your array*]}
    

    其次,每个对象/数组名称都应该是一个字符串——这意味着在引号中:

    {
        "Name": "Angus Smoked Bacon And Cheddar",
        "SandPrice": "4.99",
        "RegPrice": "6.49"
    }
    

    详情请参考The JSON Data Interchange Format

    然后您可以使用以下模板从数组中获取 JSON 对象:

    JSONArray array = new JSONObject(myFileContents).getJSONArray("Array");
    for (int i = 0; i < array.length(); i++) {
        JSONObject object = array.getJSONObject(i);
        // . . . work with object
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-17
      • 2020-12-01
      • 2012-12-08
      • 2019-12-19
      • 1970-01-01
      相关资源
      最近更新 更多