2011-07-11 70 views
0

我正在編寫一個Android程序,需要訪問一些值。我將如何訪問Chevorlet Volt在0指數下的持續時間爲120。我已經是存儲在使用一個JSONObject ..如何訪問JSON值?

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" 
       } 
      ....... 
+0

你想讀從JavaScript或Java的價值? –

+0

使用Google的GSON進行JSON解析:http://code.google.com/p/google-gson/ – binnyb

+0

您確定您的JSON格式良好嗎?我看到你的JSON數組中有鍵/值對的屬性。 –

回答

2

你嘗試過這麼遠嗎?

您可能最終會用一個庫解析數據,最有可能是已經附帶Android的org.json包。使用JSONTokener,您可以將收到的數據解析到JSONObject中,然後下降到該對象中以查找所需的確切值。

+0

我把它作爲一個JSONObject。什麼是正確的代碼下降到對象的值持續時間。 –