2016-07-30 8 views
-1

Ich habe die folgenden JSON-Daten aus meiner API:Android bekommen JSON-Array ohne Namen

enter image description here

Json-Array und eine Menge von json Objekte darin.

Wie bekomme ich ein JSON-Array ohne einen Namen? Diese

ist, was ich versucht:

//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")); 
} 
} 

die json Variable speichert alle Daten json!

+0

versuchen, das '‘ ‚' 'mit“ „', zu ersetzen, und wenn es nicht funktioniert, versuchen Sie, mit '" '' –

+0

Funktioniert nicht. Wenn ich meine API ändere und mein Array nenne und dort "myArray" eintippe, funktioniert es ... ohne einen Namen - es tut nicht: S – TheUnreal

+0

Beginnt Ihre Antwort mit diesem Array? –

Antwort

2

JSONArray hat einen Konstruktor, der eine String-Quelle akzeptiert.

JSONArray array = new JSONArray(yourJSONArrayAsString); 

Dann können Sie jedes Objekt mit for-Schleife erhalten.

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

Aber meine Antwort ist ein JSON-Objekt, ich denke .. Ich mache 'J = neue JSONObject (Antwort);' – TheUnreal

-1

Erstellen Sie die JSONParser-Klasse, die den Json-Parsing-Code enthält.

erwähnen Sie in Ihrer MainActivity.java

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

Jetzt Erstellen Sie separate JSONParser Klasse

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; 
} 

Jetzt in 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(); 
}