2016-07-30 3 views
-1

없이 JSON 배열을 얻을 내가 가지고 내 API에서 다음 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

시도를 별도의 JSONParser 클래스를 생성하고 그것이 작동하지 않는 경우에 시도 ' "" –

+0

작동하지 않습니다. API를 변경하고 배열 이름을 "myArray"라고 입력하면 작동하지 않습니다. 이름이 없으면 작동하지 않습니다. S – TheUnreal

+0

응답이이 배열로 시작합니까? –

답변

2

JSONArray에는 String 소스를 사용하는 생성자가 있습니다.

JSONArray array = new JSONArray(yourJSONArrayAsString); 

그런 다음 for 루프를 사용하여 각 개체를 가져올 수 있습니다.

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

이제` ''`` ""`으로 대체 MainActivity.java에서 이제

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

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