How to access JSON values?

Im writing an Android program and need to access some values. How would I access Duration Under the Chevorlet Volt in the 0 index for 120.? I already have it stored in a JSONObject using..

vech = context.getAssets().open("vec9.json");

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

JSON code…

{"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"
                }
           .......

Answer

There is a terrific tutorial for that using GSON

Take a look

Leave a Reply

Your email address will not be published. Required fields are marked *