2014-10-29 48 views
0

我想給..但我在收到錯誤JSON對象的靜態值。如何,我會分配一個value.I嘗試像 第一種方式如何給json對象的靜態值?

JSONObject jsonObj={"headcode":"s","destination":"Ardwick","origin":"Ardwick","time":"29:10:2014 09:52","turnNumber":"as","depot":"Barrow","conductorName":"as","devicePin":"123456.78.364813.8","noFirstClassImpact":"true","customersInvited":"false","customersUninvited":"false","customersLeft":"false"}; 

我得到誤差變化的JSONObject串陣列

回答

0

這不會像那樣工作。查看其他答案。

你也可以做這樣的:

JSONObject jsonObject = new JSONObject(); 

jsonObject.put("destination", "ardwick"); 
jsonObject.put("depot", "barrow"); 
... 

然後你可以使用jsonObject.toString()得到的值。

Here你有JSONObject的文檔。

0

試試這種方式,希望這會幫助你解決你的問題。

try { 
    JSONObject jsonObj = new JSONObject("{\"headcode\":\"s\",\"destination\":\"Ardwick\",\"origin\":\"Ardwick\",\"time\":\"29:10:2014 09:52\",\"turnNumber\":\"as\",\"depot\":\"Barrow\",\"conductorName\":\"as\",\"devicePin\":\"123456.78.364813.8\",\"noFirstClassImpact\":\"true\",\"customersInvited\":\"false\",\"customersUninvited\":\"false\",\"customersLeft\":\"false\"}"); 
}catch (JSONException e){ 
    e.printStackTrace(); 
} 
0

,這是一個完整的例子來創建一個靜態的JSONObject:

try { 
      String myJson = "{\n" + 
        " \"employees\": [\n" + 
        "  {\"first_name\": \"employeeFirstName1\", \"last_name\": \"employeeLastName1\"}, \n" + 
        "  {\"first_name\": \"employeeFirstName2\", \"last_name\": \"employeeLastName2\"}\n" + 
        " ],\n" + 
        " \"manager\": [\n" + 
        "  {\"first_name\": \"managerFirstName1\", \"last_name\": \"managerLastName1\"}, \n" + 
        "  {\"first_name\": \"managerFirstName2\", \"last_name\": \"managerLastName2\"}\n" + 
        " ]\n" + 
        "}"; 
      //Log.i(CommonCode.TAG, myJson); 
      JSONObject array = new JSONObject(myJson); 
      JSONArray employees = array.getJSONArray("employees"); 
      JSONArray managers = array.getJSONArray("manager"); 

      Log.i("myDebug", "----- EMPLOYEES -----"); 

      for (int i = 0; i < employees.length(); i++) { 
       JSONObject row = employees.getJSONObject(i); 
       String firstName = row.getString("first_name"); 
       String lastName = row.getString("last_name"); 
       Log.i(CommonCode.TAG, "First name: " + firstName + ", Last name: " + lastName); 
      } 

      Log.i("myDebug", "----- MANAGERS -----"); 

      for(int i = 0; i< managers.length(); i++){ 
       JSONObject row = managers.getJSONObject(i); 
       String firstName = row.getString("first_name"); 
       String lastName = row.getString("last_name"); 
       Log.i(CommonCode.TAG, "First name: " + firstName + ", Last name: " + lastName); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     }