2013-11-02 79 views
10

我解析json到我的android應用程序有一個小問題。Android解析JSONObject

這是我的JSON文件看起來像:

{ 
"internalName": "jerry91", 
"dataVersion": 0, 
"name": "Domin91", 
"profileIconId": 578, 
"revisionId": 0, 
} 

正如你可以看到這個結構是一個有點怪異。我不知道如何在我的應用程序中讀取這些數據。正如我注意到這些都是對象不是陣列:/

+0

更多解釋 –

回答

2

這部分AsyncTask

JSONParser jParser = new JSONParser(); 
JSONObject json = jParser.getJSONFromUrl(url); 


     try { 

      result = json.getString("internalName"); 
          data=json.getString("dataVersion"); 
         ect.. 


     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

onBackground JsonParser

public class JSONParser { 

static InputStream is = null; 
static JSONObject jObj = null; 
static String json = ""; 

// constructor 
public JSONParser() { 

} 

public JSONObject getJSONFromUrl(String url) { 

    // Making HTTP request 
    try { 
     // defaultHttpClient 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 
     HttpResponse httpResponse = httpClient.execute(httpPost); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     is = httpEntity.getContent(); 

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    try { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
       is, "utf-8"), 8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     is.close(); 
     json = sb.toString(); 
    } catch (Exception e) { 
     Log.e("Buffer Error", "Error converting result " + e.toString()); 
    } 

    // try parse the string to a JSON object 
    try { 
     jObj = new JSONObject(json); 
    } catch (JSONException e) { 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 
    } 

    // return JSON String 
    return jObj; 

} 
    } 
21

您可以隨時使用好老json.org庫。在您的Java代碼中:

  • 首先將您的json文件內容讀入String;
  • 然後解析成JSONObject

    JSONObject myJson = new JSONObject(myJsonString); 
    // use myJson as needed, for example 
    String name = myJson.optString("name"); 
    int profileIconId = myJson.optInt("profileIconId"); 
    // etc 
    
0

我建議你使用像GSON庫作爲@jmeier在他的回答中寫道。但是,如果你要處理與Android的默認JSON,你可以使用這樣的事情:

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     String s = new String("{\"internalName\": \"domin91\",\"dataVersion\": 0,\"name\": \"Domin91\",\"profileIconId\": 578,\"revisionId\": 0,}"); 

     try { 
      MyObject myObject = new MyObject(s); 
      Log.d("MY_LOG", myObject.toString()); 
     } catch (JSONException e) {   
      Log.d("MY_LOG", "ERROR:" + e.getMessage()); 
     } 


    } 

    private static class MyObject { 
     private String internalName; 
     private int dataVersion; 
     private String name; 
     private int profileIconId; 
     private int revisionId; 

     public MyObject(String jsonAsString) throws JSONException { 
      this(new JSONObject(jsonAsString)); 
     } 

     public MyObject(JSONObject jsonObject) throws JSONException { 
      this.internalName = (String) jsonObject.get("internalName"); 
      this.dataVersion = (Integer) jsonObject.get("dataVersion"); 
      this.name = (String) jsonObject.get("name"); 
      this.profileIconId = (Integer) jsonObject.get("profileIconId"); 
      this.revisionId = (Integer) jsonObject.get("revisionId"); 
     } 

     @Override 
     public String toString() { 
      return "internalName=" + internalName + 
        "dataVersion=" + dataVersion + 
        "name=" + name + 
        "profileIconId=" + profileIconId + 
        "revisionId=" + revisionId; 
     } 

    } 

} 
8

知道,如果字符串是JSONArrayJSONObject

JSONArray字符串是這樣

[{ 
"internalName": "blaaa", 
"dataVersion": 0, 
"name": "Domin91", 
"profileIconId": 578, 
"revisionId": 0, 
}, 
{ 
"internalName": "blooo", 
"dataVersion": 0, 
"name": "Domin91", 
"profileIconId": 578, 
"revisionId": 0, 
}] 

和此字符串作爲JSONOject

{ 
"internalName": "domin91", 
"dataVersion": 0, 
"name": "Domin91", 
"profileIconId": 578, 
"revisionId": 0, 
} 

但如何調用JSONArrayJSONObject中的元素?

JSNOObject信息稱爲像數據這

第一填充物

JSONObject object = new JSONObject(
"{ 
\"internalName\": \"domin91\", 
\"dataVersion\": 0, 
\"name\": \"Domin91\", 
\"profileIconId\": 578, 
\"revisionId\": 0, 
}" 
); 

現在讓我們從對象

String myusername = object.getString("internalName"); 
int dataVersion = object.getInt("dataVersion"); 

呼叫信息。如果你想調用從JSONArray信息,你必須知道是對象位置號碼還是必須循環JSONArray才能獲取信息,例如

循環數組

for (int i = 0; i < jsonarray.length() ; i++) 
{ 
    //this object inside array you can do whatever you want 
    JSONObject object = jsonarray.getJSONObject(i); 
} 

,如果我知道里面JSONArray生病的對象位置調用它

//0 mean first object inside array 
JSONObject object = jsonarray.getJSONObject(0);