2015-09-29 282 views
0

我收到服務器的響應爲JSON格式,並需要獲得單場「文本」並將其存儲在數組列表。我試過,但只獲得第一個文本字段,如下面顯示的輸出,但我需要證據列表中的所有文本字段json object.Can任何人都可以幫助我在這裏獲得所有文本字段?如何僅從JSON服務器響應中獲取「文本」?

的Android代碼:從服務器

HttpEntity entity = response.getEntity(); // wait for response 
      // convert response to a string and log it 
      if (entity != null) { 

            //Toast.makeText(RegistartionPage.this, 
         // "Inside If", Toast.LENGTH_LONG) 
         //.show(); 
      InputStream inputstream = entity.getContent(); 
      BufferedReader bufferedreader = 
      new BufferedReader(new InputStreamReader(inputstream)); 
      StringBuilder stringbuilder = new StringBuilder(); 
      String currentline = null; 
      while ((currentline = bufferedreader.readLine()) != null) { 
       //Toast.makeText(RegistartionPage.this, 
         // "Inside while", Toast.LENGTH_LONG) 
         // .show(); 
      stringbuilder.append(currentline + "\n"); 
      } 
      String result = stringbuilder.toString(); 
      Log.v("HTTP REQUEST",result); 
      inputstream.close(); 

      ids=result; 

      } 
      JSONArray jsonArray = new JSONArray(ids); 
      for (int i = 0; i < jsonArray.length(); i++) { 
       JSONObject c = jsonArray.getJSONObject(i); 
       String question1 = c.getString("question"); 
       JSONObject object2 = new JSONObject(question1); 
       JSONArray jArray1 = object2.getJSONArray("evidencelist"); 
       //String comp_id = jArray1.optString(); 
       for(int j = 0; j < jArray1 .length(); j++) 
       { 
        JSONObject object3 = jArray1.getJSONObject(j); 
        text = object3.getString("text"); 
        /*num = object3.getInt("value");*/ 

       } 
      } 

JSON輸出:

09-19 17:00:41.182: V/HTTP REQUEST(1539): [ 
09-19 17:00:41.182: V/HTTP REQUEST(1539): { 
09-19 17:00:41.182: V/HTTP REQUEST(1539): "question": { 
09-19 17:00:41.182: V/HTTP REQUEST(1539): "qclasslist": [ 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  { 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "value": "FACTOID" 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  }, 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  { 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "value": "DESCRIPTIVE" 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  } 
09-19 17:00:41.182: V/HTTP REQUEST(1539): ], 
09-19 17:00:41.182: V/HTTP REQUEST(1539): "focuslist": [ 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  { 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "value": "What" 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  } 
09-19 17:00:41.182: V/HTTP REQUEST(1539): ], 
09-19 17:00:41.182: V/HTTP REQUEST(1539): "latlist": [ 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  { 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "value": "HIV" 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  } 
09-19 17:00:41.182: V/HTTP REQUEST(1539): ], 
09-19 17:00:41.182: V/HTTP REQUEST(1539): "evidencelist": [ 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  { 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "value""0.90134596824646", 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "text": "HIV stands for human immunodeficiency virus. This is the virus that causes AIDS. There is no cure yet for HIV/AIDS, but there are treatments that can help people live longer, healthier lives.", 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "id": "PB_472D471A96B201B91949C3B1C28D468B", 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "title": "Get Tested for HIV : Get Tested for HIV : The Basics : What is HIV?", 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "document":"http://10.110.76.161:8080/instance/67/deepqa/v1/question/document/PB_472D471A96B201B91949C3B1C28D468B/175/3441" (http://10.110.76.161:8080/instance/67/deepqa/v1/question/document/PB_472D471A96B201B91949C3B1C28D468B/175/3441%27) , 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "copyright": "No copyright specified for this supporting passage or document.", 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "termsOfUse": "No license specified for this supporting passage or document.", 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "metadataMap": { 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "abstract": "Protect yourself and others from HIV and other STDs.", 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "originalfile": "Get Tested for HIV.html", 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "title": "Get Tested for HIV : Get Tested for HIV : The Basics : What is HIV?", 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "corpusName": "PB", 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "description": "Protect yourself and others from HIV and other STDs.", 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "deepqaid": "33646738", 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "fileName": "/watsondata/ingestion/14d9d1d3b23/working/201505290044/eng/di/output/trec/intermediate/xml-splitTrecTrim/33646738.xml", 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "DOCNO": "472D471A96B201B91949C3B1C28D468B" 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  } 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  }, 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  { 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "value":"0.06850375980138779", 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "text": "Breastfeeding, pregnancy, or childbirth if the mother has HIV. Getting a transfusion of blood that's infected with HIV (very rare in the United States). Learn more about HIV/AIDS: What is HIV/AIDS? Women and HIV/AIDS. Take Action! Pages: 1 2 3 4 5 6 >.", 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "id":"PB_472D471A96B201B944EEF8045EC5E0AF", 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "title": "Get Tested for HIV : Get Tested for HIV : The Basics : How do people get HIV?", 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "document":"http://10.110.76.161:8080/instance/67/deepqa/v1/question/document/PB_472D471A96B201B944EEF8045EC5E0AF/473/725" (http://10.110.76.161:8080/instance/67/deepqa/v1/question/document/PB_472D471A96B201B944EEF8045EC5E0AF/473/725%27) , 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "copyright": "No copyright specified for this supporting passage or document.", 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "termsOfUse": "No license specified for this supporting passage or document.", 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "metadataMap": { 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "abstract": "Protect yourself and others from HIV and other STDs.", 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "originalfile": "Get Tested for HIV.html", 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "title": "Get Tested for HIV : Get Tested for HIV : The Basics : How do people get HIV?", 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "corpusName": "PB", 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "description": "Protect yourself and others from HIV and other STDs.", 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "deepqaid": "33646739", 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "fileName": "/watsondata/ingestion/14d9d1d3b23/working/201505290044/eng/di/output/trec/intermediate/xml-splitTrecTrim/33646739.xml", 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "DOCNO": "472D471A96B201B944EEF8045EC5E0AF" 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  } 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  }, 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  { 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "value":"0.03481652960181236", 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "text": "See a doctor right away if you have unprotected anal or vaginal sex with someone who might be HIV-positive. Taking medication within 3 days (known as post-exposure prophylaxis or PEP) can dramatically reduce the chance of getting HIV. Talk to your doctor about HIV medication to prevent HIV infection (known as pre-exposure prophylaxis or PrEP) if you often have unprotected anal or vaginal sex. If you are HIV-positive, tell your sex partners you have HIV before you have sex with them and take steps to prevent transmitting HIV to others.", 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "id": "PB_8C42FE3B49104EC939E170D23ADBCDD7", 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "title": "National Latino AIDS Awareness Day : National Latino AIDS Awareness Day : National Latino AIDS Awareness Day :What Can You Do?", 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "document":"http://10.110.76.161:8080/instance/67/deepqa/v1/question/document/PB_8C42FE3B49104EC939E170D23ADBCDD7/1379/1919" (http://10.110.76.161:8080/instance/67/deepqa/v1/question/document/PB_8C42FE3B49104EC939E170D23ADBCDD7/1379/1919%27) , 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "copyright": "No copyright specified for this supporting passage or document.", 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "termsOfUse": "No license specified for this supporting passage or document.", 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "metadataMap": { 
09-19 17:00:41.182: V/HTTP REQUEST(1539):  "abstract": "HIV continues to threaten the health of Hispanic and Latinos in the United States. CDC 
09-19 17:00:41.249: I/Choreographer(1539): Skipped 12476 frames! The application may be doing too much work on its main thread. 
+0

https://github.com/mohit008/Android-Json-例如 – Mohit

+0

是的,我能拿到第一個文本字段即HIV代表人類,但我已經在環3個文本字段。剩餘2個文本字段無法獲取。 –

+0

你檢查了我的回答 – Mohit

回答

1

試試這個..

JSONArray jsonArray = new JSONArray(ids); 
for (int i = 0; i < jsonArray.length(); i++) { 
    JSONObject ques_obj = jsonArray.getJSONObject(i); 

    JSONObject question = ques_obj.getJSONObject("question"); 

    JSONArray q = question.getJSONArray("qclasslist"); 
    JSONArray f = question.getJSONArray("focuslist"); 
    JSONArray l = question.getJSONArray("latlist"); 

    for (int a = 0; a < q.length(); a++) { 
     JSONObject q_obj = q.getJSONObject(a); 
     text = q_obj .getString("value"); 
    } 
    for (int b = 0; b < f.length(); b++) { 
     JSONObject f_obj = f.getJSONObject(b); 
     text = f_obj.getString("value"); 

    } 

    for (int c = 0; c < l.length(); c++) { 
     JSONObject l_obj = l.getJSONObject(c); 

     text = l_obj.getString("value"); 
    } 

    JSONArray jArray1 = question.getJSONArray("evidencelist"); 

    for(int d = 0; d < jArray1 .length(); d++) 
    { 
     JSONObject object3 = jArray1.getJSONObject(d); 
     text = object3.getString("text"); 
    } 
} 
+0

是的,但我得到這個錯誤 09-29 19:42:48.620:W/System.err(3090):org.json.JSONException:沒有值的值 09-29 19:42 :48.621:W/System.err的(3090):\t在org.json.JSONObject.get(JSONObject.java:389) 09-29 19:42:48.622:W/System.err的(3090):\t在組織.json.JSONObject.getString(JSONObject.java:550) –

+0

哪個錯誤....? – Mohit

+0

否價值 –

相關問題