2015-09-18 12 views
-1

Я пытаюсь Считайте ответ JSonJsonResponse не работает должным образом

{ 
"title": "Flipkart Affiliate API Directory", 
"description": "This directory contains information about all the affiliate  API's and their versions", 
"apiGroups": { 
"affiliate": { 
    "name": "affiliate", 
    "apiListings": { 
     "bags_wallets_belts": { 
      "availableVariants": { 
       "v0.1.0": { 
        "resourceName": "bags_wallets_belts", 
        "get": "https://affiliate-api.flipkart.net/affiliate/feeds/youraffiliateid/category/v1:reh.json?expiresAt=1420910131274&sig=7db02590dfaea9a22f88c869d8035d05", 
        "post": null, 
        "put": null, 
        "delete": null 
       } 
      }, 
      "apiName": "bags_wallets_belts" 
     }, 
     "washing_machine": { 
      "availableVariants": { 
       "v0.1.0": { 
        "resourceName": "washing_machine", 
        "get": "https://affiliate-api.flipkart.net/affiliate/feeds/youraffiliateid/category/v1:j9e-abm-8qx.json?expiresAt=1420910131275&sig=7c48abab9d35ae77fff3d998e1a2efdc", 
        "post": null, 
        "put": null, 
        "delete": null 
       } 
      }, 
      "apiName": "washing_machine" 
     }, 
    } 
} 
} 

мне нужен массив «apiNames». Ниже мой класс о том, как я пытаюсь, чтобы прочитать JSon

class SearchProducts extends AsyncTask<String, Void, Integer> { 
    final List<RowItem> rowItem = new ArrayList<RowItem>(); 
    final int SUCCESS = 0; 
    final int FAILURE = SUCCESS + 1; 
    ProgressDialog dialog; 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     dialog = ProgressDialog.show(FlipkartProducts.this, "", getString(R.string.searchingflipkart)); 
    } 

    @Override 
    protected Integer doInBackground(String... params) { 
     try { 

      String urlString = "https://affiliate-api.flipkart.net/affiliate/api/royarnab8.json"; 
      URL url = new URL(urlString); 

      InputStream inputStream = url.openConnection().getInputStream(); 
      String response = streamToString(inputStream); 
      JSONObject jsonObject = (JSONObject) new JSONTokener(response).nextValue(); 
      JSONArray jsonArray = jsonObject.getJSONArray("apiGroups"); 
      int a = jsonArray.length(); 
      Log.d("The length of the array is", "" + a); 

      for (int i =0; i<jsonArray.length(); i++) { 

       JSONObject mainImageJsonObject = jsonArray.getJSONObject(i).getJSONObject("affiliate") 
         .getJSONObject("apiListings"); 
       String categories = mainImageJsonObject.getString("apiName"); 

       RowItem ri = new RowItem(categories); 
       rowItem.add(ri); 

      } 
      return SUCCESS; 

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

     return FAILURE; 
    } 

    @Override 
    protected void onPostExecute(Integer result) { 
     super.onPostExecute(result); 
     dialog.dismiss(); 
     if (result == SUCCESS) { 
      lv.setAdapter(new SavedUrlAdapter(FlipkartProducts.this,rowItem)); 
     } else { 
      Toast.makeText(FlipkartProducts.this, getString(R.string.errorfk), Toast.LENGTH_LONG).show(); 
     } 
    } 
} 
public static String streamToString(InputStream p_is) 
{ 
    try 
    { 
     BufferedReader m_br; 
     StringBuffer m_outString = new StringBuffer(); 
     m_br = new BufferedReader(new InputStreamReader(p_is)); 
     String m_read = m_br.readLine(); 
     while(m_read != null) 
     { 
      m_outString.append(m_read); 
      m_read =m_br.readLine(); 
     } 
     return m_outString.toString(); 
    } 
    catch (Exception p_ex) 
    { 
     p_ex.printStackTrace(); 
     return ""; 
    } 
} 

После запуска программы, я получаю «apiGroups» jsonArray. Но после этого ничего не происходит. Может ли кто-нибудь помочь мне решить эту проблему?

+0

"apiGroups" не является JSONArray – Blackbelt

ответ

1

Попробуйте это:

JSONObject groupObject = jsonObject.getJSONObject("apiGroups"); 

String apiName =groupObject.getJSONObject("affiliate").getJSONObject("apiListings").getJSONObject("bags_wallets_belts").getString("apiName"); 

Чтобы быть более точным для всех объектов под apiListings:

JSONObject apiListingObject = jsonObject.getJSONObject("apiGroups").getJSONObject("affiliate").getJSONObject("apiListings"); 
JSONObject obj4=null; 
     //Getting all the keys inside json object with key- pages 
      Iterator<String> keys= apiListingObject.keys(); 
      while (keys.hasNext()) 
     { 
       String keyValue = (String)keys.next(); 
       obj4 = apiListingObject.getJSONObject(keyValue); 
       //getting string values with keys- apiName 
       String pageid = obj4.getString("apiName");   
     } 
+0

что это obj3 означает? – Arnab

+0

obj3 изменяется после каждой итерации, на первой итерации obj3 будет JSONObject с ключом bag_wallets_belts, а на второй итерации obj3 будет JSONObject с ключом wash_machine – Aakash

+0

- ваша проблема решена или нет? – Aakash

Смежные вопросы