2016-07-30 45 views
-1

我從我的API以下JSON數據:的Android得JSON數組沒有名字

enter image description here

JSON數組和它JSON對象很多。

如何得到一個沒有名字的json數組?

這是我的嘗試:

//Parsing the fetched Json String to JSON Object 
j = new JSONObject(response); 
result = j.getJSONArray(''); 
for(int i=0;i<j.length();i++){ 
      try { 
       //Getting json object 
       JSONObject json = j.getJSONObject(i); 
       Log.d("tag", "NAME IS: " +json.getString("name")); 
} 
} 

json變量存儲所有的JSON數據!

+0

嘗試'」「'和'」「'來代替,如果它不能正常工作與嘗試'「」' –

+0

不起作用。如果我正在更改我的API並命名我的數組,並在其中鍵入「myArray」,它將起作用..沒有名稱 - 它不會:S – TheUnreal

+0

您的響應是否以此數組開頭? –

回答

2

JSONArray有一個構造函數,它接受一個String源。

JSONArray array = new JSONArray(yourJSONArrayAsString); 

然後你可以得到每個對象使用循環。

JSONArray array; 
    try { 
     array = new JSONArray(yourJSONArrayAsString); 

     for(int i=0;i<array.length();i++){ 
      JSONObject obj = array.getJSONObject(i); 
      // get your data from jsonobject 
     } 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
+0

但我的迴應是一個json對象,我猜...我做'j = new JSONObject(response);' – TheUnreal

-1

創建包含json解析代碼的JSONParser類。

提你MainActivity.java

JSONParser jsonparser =新JSONParser(); JSONObject jsonObject = jsonparser.getJSONFromURL(URL);

立即創建單獨JSONParser類

class JSONParser 
{ 
    public static JSONObject jsonObject=null; 
    public static String json=null; 
    InputStream is=null; 
    public JSONObject getJSONFromURL(String url) 
{ 
    try 
{ 
     HttpClient client=new DefaultHttpClient(); 
     HttpPost post=new HttpPost(url); 
     HttpResponse response=client.execute(post); 
     HttpEntity entity=response.getEntity(); 
     is=entity.getContent(); 
} 
catch(Exception e) 
{ 
    e.printStackTrace(); 
} 
try 
{ 
    BufferedReader br=new BufferedReader(new InputStreamReader(is,"UTF-8")); 
    StringBuilder sb=-new StringBuilder(); 
    String line=null; 
    while((br.readLine())!=null) 
    { 
    sb.append(line+"\n"); 
    } 
json=sb.toString(); 
is.close(); 
} 
catch(Exception e) 
{ 
    e.printStackTrace(); 
} 
try 
{ 
    jsonObject=new JSONObject(json.subString(json.indexOf("{"),json.lastinddexOf("}")+1)); 
} 
catch(Exception e) 
{ 
    e.printStackTrace(); 
} 
return jsonObject; 
} 

現在MainActivity.java

try 
{ 
JSONParser jsonparser=new JSONParser(); 
JSONObject jsonObject=jsonparser.getJSONFromURL(URL);// URL is a String which contains url 
Log.d("Response:",jsonObject.toString()); 
JSONArray jsonarray=new JSONArray(jsonObject.getString("YourFirstJSONArrayName"));//YourJSONArray contains the response array 
for(int i=0;i<jsonarray.length();i++) 
{ 
    JSONObject c=jsonarray.getJSONObject(i); 
    // now get data from c object 
} 
// Now getting data from Second Array 
JSONArray jsona=new JSONArray(jsonObject.getString("YourSecondJSONArrayName")); 
    for(int j=0;j<jsona.length();j++) 
{ 
    JSONObject c=jsona.getJSONObject(j); 
     // now get data from json data from second array 
} 
} 
catch(Exception e) 
{ 
    e.printStackTrace(); 
}