如何访问JSON值?

时间:2011-07-11 13:13:33

标签: java javascript android eclipse

我正在编写一个Android程序,需要访问一些值。我将如何在0指数中访问Chevorlet Volt的持续时间为120。我已经使用..

将它存储在JSONObject中
vech = context.getAssets().open("vec9.json");

        String s = convertStreamToString(vech);
        try {
            Vehicles = new JSONObject(s);
        } catch (JSONException e) {
            e.printStackTrace();
        }

JSON代码......

{"root": {
"FORM": {
  "FORM_INIT": {
    "@attributes": {
      "mdd": "20",
      "cost": "2.85",
      "mpg": "24"
    }
  }
},
"VEHICLES": {
  "VEHICLE": [
    {
      "@attributes": {
        "index": "0",
        "desc": "Chevrolet Volt",
        "energy": "12",
        "sac": "72",
        "divisor": "40"
      },
      "KWH": [
        {
          "@attributes": {
            "index": "0",
            "level": "120",
            "duration": "10"
          },
          "KWH_PCT": {
            "START": [
              {
                "@attributes": {
                  "time": "0",
                  "sum_idx_0": "70",
                  "sum_idx_1": "30",
                  "sum_idx_2": "0",
                  "win_idx_0": "70",
                  "win_idx_2": "30"
                }
              },
              {
                "@attributes": {
                  "time": "1",
                  "sum_idx_0": "60",
                  "sum_idx_1": "40",
                  "sum_idx_2": "0",
                  "win_idx_0": "60",
                  "win_idx_2": "40"
                }
              },
              {
                "@attributes": {
                  "time": "2",
                  "sum_idx_0": "50",
                  "sum_idx_1": "50",
                  "sum_idx_2": "0",
                  "win_idx_0": "50",
                  "win_idx_2": "50"
                }
              },
              {
                "@attributes": {
                  "time": "3",
                  "sum_idx_0": "40",
                  "sum_idx_1": "60",
                  "sum_idx_2": "0",
                  "win_idx_0": "40",
                  "win_idx_2": "60"
                }
              },
              {
                "@attributes": {
                  "time": "4",
                  "sum_idx_0": "30",
                  "sum_idx_1": "70",
                  "sum_idx_2": "0",
                  "win_idx_0": "30",
                  "win_idx_2": "70"
                }
              },
              {
                "@attributes": {
                  "time": "5",
                  "sum_idx_0": "20",
                  "sum_idx_1": "70",
                  "sum_idx_2": "10",
                  "win_idx_0": "20",
                  "win_idx_2": "80"
                }
              },
              {
                "@attributes": {
                  "time": "6",
                  "sum_idx_0": "10",
                  "sum_idx_1": "70",
                  "sum_idx_2": "20",
                  "win_idx_0": "10",
                  "win_idx_2": "90"
                }
              },
              {
                "@attributes": {
                  "time": "7",
                  "sum_idx_0": "0",
                  "sum_idx_1": "70",
                  "sum_idx_2": "30",
                  "win_idx_0": "0",
                  "win_idx_2": "100"
                }
              },
              {
                "@attributes": {
                  "time": "8",
                  "sum_idx_0": "0",
                  "sum_idx_1": "60",
                  "sum_idx_2": "40",
                  "win_idx_0": "0",
                  "win_idx_2": "100"
                }
              },
              {
                "@attributes": {
                  "time": "9",
                  "sum_idx_0": "0",
                  "sum_idx_1": "60",
                  "sum_idx_2": "40",
                  "win_idx_0": "0",
                  "win_idx_2": "100"
                }
              },
              {
                "@attributes": {
                  "time": "10",
                  "sum_idx_0": "0",
                  "sum_idx_1": "60",
                  "sum_idx_2": "40",
                  "win_idx_0": "0",
                  "win_idx_2": "100"
                }
              },
              {
                "@attributes": {
                  "time": "11",
                  "sum_idx_0": "0",
                  "sum_idx_1": "60",
                  "sum_idx_2": "40",
                  "win_idx_0": "0",
                  "win_idx_2": "100"
                }
              },
              {
                "@attributes": {
                  "time": "12",
                  "sum_idx_0": "0",
                  "sum_idx_1": "60",
                  "sum_idx_2": "40",
                  "win_idx_0": "0",
                  "win_idx_2": "100"
                }
              },
              {
                "@attributes": {
                  "time": "13",
                  "sum_idx_0": "0",
                  "sum_idx_1": "60",
                  "sum_idx_2": "40",
                  "win_idx_0": "0",
                  "win_idx_2": "100"
                }
              },
              {
                "@attributes": {
                  "time": "14",
                  "sum_idx_0": "10",
                  "sum_idx_1": "50",
                  "sum_idx_2": "40",
                  "win_idx_0": "10",
                  "win_idx_2": "90"
                }
              },
              {
                "@attributes": {
                  "time": "15",
                  "sum_idx_0": "20",
                  "sum_idx_1": "50",
                  "sum_idx_2": "30",
                  "win_idx_0": "20",
                  "win_idx_2": "80"
                }
              },
              {
                "@attributes": {
                  "time": "16",
                  "sum_idx_0": "30",
                  "sum_idx_1": "50",
                  "sum_idx_2": "20",
                  "win_idx_0": "30",
                  "win_idx_2": "70"
                }
              },
              {
                "@attributes": {
                  "time": "17",
                  "sum_idx_0": "40",
                  "sum_idx_1": "50",
                  "sum_idx_2": "10",
                  "win_idx_0": "40",
                  "win_idx_2": "60"
                }
              },
              {
                "@attributes": {
                  "time": "18",
                  "sum_idx_0": "50",
                  "sum_idx_1": "50",
                  "sum_idx_2": "0",
                  "win_idx_0": "50",
                  "win_idx_2": "50"
                }
              },
              {
                "@attributes": {
                  "time": "19",
                  "sum_idx_0": "60",
                  "sum_idx_1": "40",
                  "sum_idx_2": "0",
                  "win_idx_0": "60",
                  "win_idx_2": "40"
                }
              },
              {
                "@attributes": {
                  "time": "20",
                  "sum_idx_0": "70",
                  "sum_idx_1": "30",
                  "sum_idx_2": "0",
                  "win_idx_0": "70",
                  "win_idx_2": "30"
                }
              },
              {
                "@attributes": {
                  "time": "21",
                  "sum_idx_0": "80",
                  "sum_idx_1": "20",
                  "sum_idx_2": "0",
                  "win_idx_0": "80",
                  "win_idx_2": "20"
                }
              },
              {
                "@attributes": {
                  "time": "22",
                  "sum_idx_0": "80",
                  "sum_idx_1": "20",
                  "sum_idx_2": "0",
                  "win_idx_0": "80",
                  "win_idx_2": "20"
                }
              },
              {
                "@attributes": {
                  "time": "23",
                  "sum_idx_0": "80",
                  "sum_idx_1": "20",
                  "sum_idx_2": "0",
                  "win_idx_0": "80",
                  "win_idx_2": "20"
                }
              }
            ]
          }
        },
        {
          "@attributes": {
            "index": "1",
            "level": "240",
            "duration": "4"
          },
          "KWH_PCT": {
            "START": [
              {
                "@attributes": {
                  "time": "0",
                  "sum_idx_0": "100",
                  "sum_idx_1": "0",
                  "sum_idx_2": "0",
                  "win_idx_0": "100",
                  "win_idx_2": "0"
                }
              },
              {
                "@attributes": {
                  "time": "1",
                  "sum_idx_0": "100",
                  "sum_idx_1": "0",
                  "sum_idx_2": "0",
                  "win_idx_0": "100",
                  "win_idx_2": "0"
                }
              },
              {
                "@attributes": {
                  "time": "2",
                  "sum_idx_0": "100",
                  "sum_idx_1": "0",
                  "sum_idx_2": "0",
                  "win_idx_0": "100",
                  "win_idx_2": "0"
                }
              },
              {
                "@attributes": {
                  "time": "3",
                  "sum_idx_0": "100",
                  "sum_idx_1": "0",
                  "sum_idx_2": "0",
                  "win_idx_0": "100",
                  "win_idx_2": "0"
                }
              },
              {
                "@attributes": {
                  "time": "4",
                  "sum_idx_0": "75",
                  "sum_idx_1": "25",
                  "sum_idx_2": "0",
                  "win_idx_0": "75",
                  "win_idx_2": "25"
                }
              },
              {
                "@attributes": {
                  "time": "5",
                  "sum_idx_0": "50",
                  "sum_idx_1": "50",
                  "sum_idx_2": "0",
                  "win_idx_0": "50",
                  "win_idx_2": "50"
                }
              },
              {
                "@attributes": {
                  "time": "6",
                  "sum_idx_0": "25",
                  "sum_idx_1": "75",
                  "sum_idx_2": "0",
                  "win_idx_0": "25",
                  "win_idx_2": "75"
                }
              },
              {
                "@attributes": {
                  "time": "7",
                  "sum_idx_0": "0",
                  "sum_idx_1": "100",
                  "sum_idx_2": "0",
                  "win_idx_0": "0",
                  "win_idx_2": "100"
                }
              },
              {
                "@attributes": {
                  "time": "8",
                  "sum_idx_0": "0",
                  "sum_idx_1": "100",
                  "sum_idx_2": "0",
                  "win_idx_0": "0",
                  "win_idx_2": "100"
                }
              },
              {
                "@attributes": {
                  "time": "9",
                  "sum_idx_0": "0",
                  "sum_idx_1": "100",
                  "sum_idx_2": "0",
                  "win_idx_0": "0",
                  "win_idx_2": "100"
                }
              },
              {
                "@attributes": {
                  "time": "10",
                  "sum_idx_0": "0",
                  "sum_idx_1": "100",
                  "sum_idx_2": "0",
                  "win_idx_0": "0",
                  "win_idx_2": "100"
                }
              },
              {
                "@attributes": {
                  "time": "11",
                  "sum_idx_0": "0",
                  "sum_idx_1": "75",
                  "sum_idx_2": "25",
                  "win_idx_0": "0",
                  "win_idx_2": "100"
                }
              },
              {
                "@attributes": {
                  "time": "12",
                  "sum_idx_0": "0",
                  "sum_idx_1": "50",
                  "sum_idx_2": "50",
                  "win_idx_0": "0",
                  "win_idx_2": "100"
                }
              },
              {
                "@attributes": {
                  "time": "13",
                  "sum_idx_0": "0",
                  "sum_idx_1": "25",
                  "sum_idx_2": "75",
                  "win_idx_0": "0",
                  "win_idx_2": "100"
                }
              },
              {
                "@attributes": {
                  "time": "14",
                  "sum_idx_0": "0",
                  "sum_idx_1": "0",
                  "sum_idx_2": "100",
                  "win_idx_0": "0",
                  "win_idx_2": "100"
                }
              },
              {
                "@attributes": {
                  "time": "15",
                  "sum_idx_0": "0",
                  "sum_idx_1": "25",
                  "sum_idx_2": "75",
                  "win_idx_0": "0",
                  "win_idx_2": "100"
                }
              },
              {
                "@attributes": {
                  "time": "16",
                  "sum_idx_0": "0",
                  "sum_idx_1": "50",
                  "sum_idx_2": "50",
                  "win_idx_0": "0",
                  "win_idx_2": "100"
                }
              },
              {
                "@attributes": {
                  "time": "17",
                  "sum_idx_0": "0",
                  "sum_idx_1": "75",
                  "sum_idx_2": "25",
                  "win_idx_0": "0",
                  "win_idx_2": "100"
                }
              },
              {
                "@attributes": {
                  "time": "18",
                  "sum_idx_0": "0",
                  "sum_idx_1": "100",
                  "sum_idx_2": "0",
                  "win_idx_0": "0",
                  "win_idx_2": "100"
                }
              },
              {
                "@attributes": {
                  "time": "19",
                  "sum_idx_0": "0",
                  "sum_idx_1": "100",
                  "sum_idx_2": "0",
                  "win_idx_0": "0",
                  "win_idx_2": "100"
                }
              },
              {
                "@attributes": {
                  "time": "20",
                  "sum_idx_0": "25",
                  "sum_idx_1": "75",
                  "sum_idx_2": "0",
                  "win_idx_0": "25",
                  "win_idx_2": "75"
                }
              },
              {
                "@attributes": {
                  "time": "21",
                  "sum_idx_0": "50",
                  "sum_idx_1": "50",
                  "sum_idx_2": "0",
                  "win_idx_0": "50",
                  "win_idx_2": "50"
                }
              },
              {
                "@attributes": {
                  "time": "22",
                  "sum_idx_0": "75",
                  "sum_idx_1": "25",
                  "sum_idx_2": "0",
                  "win_idx_0": "75",
                  "win_idx_2": "25"
                }
              },
              {
                "@attributes": {
                  "time": "23",
                  "sum_idx_0": "100",
                  "sum_idx_1": "0",
                  "sum_idx_2": "0",
                  "win_idx_0": "100",
                  "win_idx_2": "0"
                }
              }
            ]
          }
        }
      ]
    },
    {
      "@attributes": {
        "index": "1",
        "desc": "Nissan Leaf",
        "energy": "24",
        "sac": "72",
        "divisor": "100"
      },
      "KWH": [
        {
          "@attributes": {
            "index": "0",
            "level": "120",
            "duration": "20"
          },
          "KWH_PCT": {
            "START": [
              {
                "@attributes": {
                  "time": "0",
                  "sum_idx_0": "35",
                  "sum_idx_1": "45",
                  "sum_idx_2": "20",
                  "win_idx_0": "35",
                  "win_idx_2": "65"
                }
              },
              {
                "@attributes": {
                  "time": "1",
                  "sum_idx_0": "30",
                  "sum_idx_1": "50",
                  "sum_idx_2": "20",
                  "win_idx_0": "30",
                  "win_idx_2": "70"
                }
              },
              {
                "@attributes": {
                  "time": "2",
                  "sum_idx_0": "25",
                  "sum_idx_1": "55",
                  "sum_idx_2": "20",
                  "win_idx_0": "25",
                  "win_idx_2": "75"
                }
              },
              {
                "@attributes": {
                  "time": "3",
                  "sum_idx_0": "20",
                  "sum_idx_1": "60",
                  "sum_idx_2": "20",
                  "win_idx_0": "20",
                  "win_idx_2": "80"
                }
              },
              {
                "@attributes": {
                  "time": "4",
                  "sum_idx_0": "20",
                  "sum_idx_1": "60",
                  "sum_idx_2": "20",
                  "win_idx_0": "20",
                  "win_idx_2": "80"
                }
              },
              {
                "@attributes": {
                  "time": "5",
                  "sum_idx_0": "20",
                  "sum_idx_1": "60",
                  "sum_idx_2": "20",
                  "win_idx_0": "20",
                  "win_idx_2": "80"
                }
              },
              {
                "@attributes": {
                  "time": "6",
                  "sum_idx_0": "20",
                  "sum_idx_1": "60",
                  "sum_idx_2": "20",
                  "win_idx_0": "20",
                  "win_idx_2": "80"
                }
              },
              {
                "@attributes": {
                  "time": "7",
                  "sum_idx_0": "20",
                  "sum_idx_1": "60",
                  "sum_idx_2": "20",
                  "win_idx_0": "20",
                  "win_idx_2": "80"
                }
              },
              {
                "@attributes": {
                  "time": "8",
                  "sum_idx_0": "25",
                  "sum_idx_1": "55",
                  "sum_idx_2": "20",
                  "win_idx_0": "25",
                  "win_idx_2": "75"
                }
              },
              {
                "@attributes": {
                  "time": "9",
                  "sum_idx_0": "30",
                  "sum_idx_1": "50",
                  "sum_idx_2": "20",
                  "win_idx_0": "30",
                  "win_idx_2": "70"
                }
              },
              {
                "@attributes": {
                  "time": "10",
                  "sum_idx_0": "35",
                  "sum_idx_1": "45",
                  "sum_idx_2": "20",
                  "win_idx_0": "35",
                  "win_idx_2": "65"
                }
              },
              {
                "@attributes": {
                  "time": "11",
                  "sum_idx_0": "40",
                  "sum_idx_1": "40",
                  "sum_idx_2": "20",
                  "win_idx_0": "40",
                  "win_idx_2": "60"
                }
              },
              {
                "@attributes": {
                  "time": "12",
                  "sum_idx_0": "40",
                  "sum_idx_1": "40",
                  "sum_idx_2": "20",
                  "win_idx_0": "40",
                  "win_idx_2": "60"
                }
              },
              {
                "@attributes": {
                  "time": "13",
                  "sum_idx_0": "40",
                  "sum_idx_1": "40",
                  "sum_idx_2": "20",
                  "win_idx_0": "40",
                  "win_idx_2": "60"
                }
              },
              {
                "@attributes": {
                  "time": "14",
                  "sum_idx_0": "40",
                  "sum_idx_1": "40",
                  "sum_idx_2": "20",
                  "win_idx_0": "40",
                  "win_idx_2": "60"
                }
              },
              {
                "@attributes": {
                  "time": "15",
                  "sum_idx_0": "40",
                  "sum_idx_1": "45",
                  "sum_idx_2": "15",
                  "win_idx_0": "40",
                  "win_idx_2": "60"
                }
              },
              {
                "@attributes": {
                  "time": "16",
                  "sum_idx_0": "40",
                  "sum_idx_1": "50",
                  "sum_idx_2": "10",
                  "win_idx_0": "40",
                  "win_idx_2": "60"
                }
              },
              {
                "@attributes": {
                  "time": "17",
                  "sum_idx_0": "40",
                  "sum_idx_1": "55",
                  "sum_idx_2": "5",
                  "win_idx_0": "40",
                  "win_idx_2": "60"
                }
              },
              {
                "@attributes": {
                  "time": "18",
                  "sum_idx_0": "40",
                  "sum_idx_1": "60",
                  "sum_idx_2": "0",
                  "win_idx_0": "40",
                  "win_idx_2": "60"
                }
              },
              {
                "@attributes": {
                  "time": "19",
                  "sum_idx_0": "40",
                  "sum_idx_1": "55",
                  "sum_idx_2": "5",
                  "win_idx_0": "40",
                  "win_idx_2": "60"
                }
              },
              {
                "@attributes": {
                  "time": "20",
                  "sum_idx_0": "40",
                  "sum_idx_1": "50",
                  "sum_idx_2": "10",
                  "win_idx_0": "40",
                  "win_idx_2": "60"
                }
              },
              {
                "@attributes": {
                  "time": "21",
                  "sum_idx_0": "40",
                  "sum_idx_1": "45",
                  "sum_idx_2": "15",
                  "win_idx_0": "40",
                  "win_idx_2": "60"
                }
              },
              {
                "@attributes": {
                  "time": "22",
                  "sum_idx_0": "40",
                  "sum_idx_1": "40",
                  "sum_idx_2": "20",
                  "win_idx_0": "40",
                  "win_idx_2": "60"
                }
              },
              {
                "@attributes": {
                  "time": "23",
                  "sum_idx_0": "40",
                  "sum_idx_1": "40",
                  "sum_idx_2": "20",
                  "win_idx_0": "40",
                  "win_idx_2": "60"
                }
              }
            ]
          }
        },
        {
          "@attributes": {
            "index": "1",
            "level": "240",
            "duration": "8"
          },
          "KWH_PCT": {
            "START": [
              {
                "@attributes": {
                  "time": "0",
                  "sum_idx_0": "87",
                  "sum_idx_1": "13",
                  "sum_idx_2": "0",
                  "win_idx_0": "87",
                  "win_idx_2": "13"
                }
              },
              {
                "@attributes": {
                  "time": "1",
                  "sum_idx_0": "75",
                  "sum_idx_1": "25",
                  "sum_idx_2": "0",
                  "win_idx_0": "75",
                  "win_idx_2": "25"
                }
              },
              {
                "@attributes": {
                  "time": "2",
                  "sum_idx_0": "63",
                  "sum_idx_1": "37",
                  "sum_idx_2": "0",
                  "win_idx_0": "63",
                  "win_idx_2": "37"
                }
              },
              {
                "@attributes": {
                  "time": "3",
                  "sum_idx_0": "50",
                  "sum_idx_1": "50",
                  "sum_idx_2": "0",
                  "win_idx_0": "50",
                  "win_idx_2": "50"
                }
              },
              {
                "@attributes": {
                  "time": "4",
                  "sum_idx_0": "37",
                  "sum_idx_1": "63",
                  "sum_idx_2": "0",
                  "win_idx_0": "37",
                  "win_idx_2": "63"
                }
              },
              {
                "@attributes": {
                  "time": "5",
                  "sum_idx_0": "25",
                  "sum_idx_1": "75",
                  "sum_idx_2": "0",
                  "win_idx_0": "25",
                  "win_idx_2": "75"
                }
              },
              {
                "@attributes": {
                  "time": "6",
                  "sum_idx_0": "13",
                  "sum_idx_1": "87",
                  "sum_idx_2": "0",
                  "win_idx_0": "13",
                  "win_idx_2": "87"
                }
           .......

2 个答案:

答案 0 :(得分:2)

到目前为止你尝试了什么?

您可能最终会使用库解析数据,很可能是Android附带的org.json包。使用JSONTokener,您可以将收到的数据解析为JSONObject,然后下载到该对象中以查找所需的确切值。

答案 1 :(得分:2)

使用GSON有一个很棒的教程

Take a look