从JSON服务器响应申请

从JSON服务器响应申请

本文介绍了如何获得只"文字"从JSON服务器响应申请?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到服务器响应JSON格式,并且需要获得单场文本并将其存储在数组列表。我试过,但我只得到第一个文本框,如下输出节目,但我需要在evidencelist JSON的所有文本字段object.Can谁能帮助我在这里把所有的文本字段?

Android的code:

  HttpEntity实体= response.getEntity(); //等待回应
            //响应转换成字符串并将其记录
            如果(实体!= NULL){                                    //Toast.makeText(RegistartionPage.this,
                      //里面如果Toast.LENGTH_LONG)
                        //。显示();
            为InputStream的InputStream = entity.getContent();
            的BufferedReader的BufferedReader =
            新的BufferedReader(新的InputStreamReader(InputStream的));
            StringBuilder的StringBuilder的=新的StringBuilder();
            字符串currentline = NULL;
            而((currentline = bufferedreader.readLine())!= NULL){
                //Toast.makeText(RegistartionPage.this,
                       //内,而Toast.LENGTH_LONG)
                       // 。显示();
            stringbuilder.append(currentline +\\ n);
            }
            字符串结果= stringbuilder.toString();
            Log.v(HTTP请求,结果);
            inputstream.close();            IDS =结果;            }
            JSONArray jsonArray =新JSONArray(IDS);
            的for(int i = 0; I< jsonArray.length();我++){
                 JSONObject的C = jsonArray.getJSONObject(I)
                 字符串问题1 = c.getString(问题);
                 的JSONObject = Object2的新的JSONObject(问题1);
                 JSONArray jArray1 = object2.getJSONArray(evidencelist);
                 //串comp_id = jArray1.optString();
                 对于(INT J = 0; J< jArray1。长度(); J ++)
                 {
                    JSONObject的object3 = jArray1.getJSONObject(J);
                    文字= object3.getString(文字);
                    / * NUM = object3.getInt(值); * /                 }
            }

从服务器输出JSON:

  09-19 17:00:41.182:V / HTTP请求(1539):
09-19 17:00:41.182:V / HTTP请求(1539):{
09-19 17:00:41.182:V / HTTP请求(1539):问题:{
09-19 17:00:41.182:V / HTTP请求(1539):qclasslist:[
09-19 17:00:41.182:V / HTTP请求(1539):{
09-19 17:00:41.182:V / HTTP请求(1539):值:的factoid
09-19 17:00:41.182:V / HTTP请求(1539):},
09-19 17:00:41.182:V / HTTP请求(1539):{
09-19 17:00:41.182:V / HTTP请求(1539):值:说明性
09-19 17:00:41.182:V / HTTP请求(1539):}
09-19 17:00:41.182:V / HTTP请求(1539)],
09-19 17:00:41.182:V / HTTP请求(1539):focuslist:[
09-19 17:00:41.182:V / HTTP请求(1539):{
09-19 17:00:41.182:V / HTTP请求(1539):值:什么?
09-19 17:00:41.182:V / HTTP请求(1539):}
09-19 17:00:41.182:V / HTTP请求(1539)],
09-19 17:00:41.182:V / HTTP请求(1539):latlist:[
09-19 17:00:41.182:V / HTTP请求(1539):{
09-19 17:00:41.182:V / HTTP请求(1539):值:HIV
09-19 17:00:41.182:V / HTTP请求(1539):}
09-19 17:00:41.182:V / HTTP请求(1539)],
09-19 17:00:41.182:V / HTTP请求(1539):evidencelist:[
09-19 17:00:41.182:V / HTTP请求(1539):{
09-19 17:00:41.182:V / HTTP请求(1539):值,0.90134596824646
09-19 17:00:41.182:V / HTTP请求(1539):文:HIV代表人类免疫缺陷病毒这是导致艾滋病的病毒有没有治愈但艾滋病毒/艾滋病,但也有。治疗方法可以帮助人们活得更长,更健康的生活。
09-19 17:00:41.182:V / HTTP请求(1539):ID:PB_472D471A96B201B91949C3B1C28D468B
09-19 17:00:41.182:V / HTTP请求(1539)称号:得到测试对于HIV:得到测试对于HIV:基础知识:什么是艾滋病,
09-19 17:00:41.182:V / HTTP请求(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请求(1539):版权:没有找到支撑通道或文档指定的版权,
09-19 17:00:41.182:V / HTTP请求(1539):termsOfUse:没有找到支撑通道或文档指定许可证,
09-19 17:00:41.182:V / HTTP请求(1539):metadataMap:{
09-19 17:00:41.182:V / HTTP请求(1539):抽象:艾滋病毒和其他性病保护自己和他人,
09-19 17:00:41.182:V / HTTP请求(1539):originalfile:得到测试对于HIV.html
09-19 17:00:41.182:V / HTTP请求(1539)称号:得到测试对于HIV:得到测试对于HIV:基础知识:什么是艾滋病,
 09-19 17:00:41.182:V / HTTP请求(1539):corpusName:PB,
 09-19 17:00:41.182:V / HTTP请求(1539):说明:保护自己和他人免受艾滋病毒和其他性病
 09-19 17:00:41.182:V / HTTP请求(1539):deepqaid:33646738,
 09-19 17:00:41.182:V / HTTP请求(1539):文件名: \"/watsondata/ingestion/14d9d1d3b23/working/201505290044/eng/di/output/trec/intermediate/xml-splitTrecTrim/33646738.xml\",
 09-19 17:00:41.182:V / HTTP请求(1539):DOCNO:472D471A96B201B91949C3B1C28D468B
 09-19 17:00:41.182:V / HTTP请求(1539):}
 09-19 17:00:41.182:V / HTTP请求(1539):},
 09-19 17:00:41.182:V / HTTP请求(1539):{
 09-19 17:00:41.182:V / HTTP请求(1539):值:0.06850375980138779
 09-19 17:00:41.182:V / HTTP请求(1539):文:母乳喂养,pregnancy,或分娩如果母亲是艾滋病感染者获得的是艾滋病病毒感染者(非常罕见的血液输血。 ,美国),了解更多关于艾滋病毒/艾滋病:什么是艾滋病毒/艾滋病的妇女和艾滋病毒/艾滋病采取行动页:?!1 2 3 4 5 6。
09-19 17:00:41.182:V / HTTP请求(1539):ID:PB_472D471A96B201B944EEF8045EC5E0AF
09-19 17:00:41.182:V / HTTP请求(1539)称号:得到测试对于HIV:得到测试对于HIV:基础知识:人们怎样HIV,
09-19 17:00:41.182:V / HTTP请求(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请求(1539):版权:没有找到支撑通道或文档指定的版权,
09-19 17:00:41.182:V / HTTP请求(1539):termsOfUse:没有找到支撑通道或文档指定许可证,
09-19 17:00:41.182:V / HTTP请求(1539):metadataMap:{
09-19 17:00:41.182:V / HTTP请求(1539):抽象:艾滋病毒和其他性病保护自己和他人,
09-19 17:00:41.182:V / HTTP请求(1539):originalfile:得到测试对于HIV.html
09-19 17:00:41.182:V / HTTP请求(1539)称号:得到测试对于HIV:得到测试对于HIV:基础知识:人们怎样HIV,
09-19 17:00:41.182:V / HTTP请求(1539):corpusName:PB,
09-19 17:00:41.182:V / HTTP请求(1539):说明:保护自己和他人免受艾滋病毒和其他性病
09-19 17:00:41.182:V / HTTP请求(1539):deepqaid:33646739,
09-19 17:00:41.182:V / HTTP请求(1539):文件名: \"/watsondata/ingestion/14d9d1d3b23/working/201505290044/eng/di/output/trec/intermediate/xml-splitTrecTrim/33646739.xml\",
09-19 17:00:41.182:V / HTTP请求(1539):DOCNO:472D471A96B201B944EEF8045EC5E0AF
09-19 17:00:41.182:V / HTTP请求(1539):}
09-19 17:00:41.182:V / HTTP请求(1539):},
09-19 17:00:41.182:V / HTTP请求(1539):{
09-19 17:00:41.182:V / HTTP请求(1539):值:0.03481652960181236
09-19 17:00:41.182:V / HTTP请求(1539):文:看医生立即如果你没有保护的肛交或阴道性交的人谁可能是艾滋病毒抗体阳性3天内服用药物(。被称为暴露后预防或PEP)可以大大减少感染艾滋病病毒的机会。告诉你的医生对艾滋病毒药​​物,以prevent HIV感染(俗称pre-暴露后预防或preP),如果你经常没有保护的肛交或阴道性交,如果你是HIV阳性,告诉你有HIV你与他们发生性关系,并采取措施,以prevent传播HIV给别人之前,你的性伙伴。
 09-19 17:00:41.182:V / HTTP请求(1539):ID:PB_8C42FE3B49104EC939E170D23ADBCDD7
 09-19 17:00:41.182:V / HTTP请求(1539)称号:全国拉丁裔艾滋病宣传日:全国拉丁裔艾滋病宣传日:全国拉丁裔艾滋病宣传日:你可以做些什么
 09-19 17:00:41.182:V / HTTP请求(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请求(1539):版权:没有找到支撑通道或文档指定的版权,
09-19 17:00:41.182:V / HTTP请求(1539):termsOfUse:没有找到支撑通道或文档指定许可证,
09-19 17:00:41.182:V / HTTP请求(1539):metadataMap:{
09-19 17:00:41.182:V / HTTP请求(1539):抽象:HIV继续威胁着拉美裔和拉美裔在美国的健康疾病预防控制中心。
09-19 17:00:41.249:I /编舞(1539):跳过12476帧!该应用程序可能会做它的主线程的工作太多了。


解决方案

试试这个..

  JSONArray jsonArray =新JSONArray(IDS);
的for(int i = 0; I< jsonArray.length();我++){
     JSONObject的ques_obj = jsonArray.getJSONObject(I)     JSONObject的问题= ques_obj.getJSONObject(问题);     JSONArray Q = question.getJSONArray(qclasslist);
     JSONArray F = question.getJSONArray(focuslist);
     JSONArray L = question.getJSONArray(latlist);     对于(int类型的= 0; A< q.length(); A ++){
         的JSONObject q_obj = q.getJSONObject(一);
         文= q_obj .getString(值);
     }
     对于(INT B = 0; B< f.length(); B ++){
         JSONObject的f_obj = f.getJSONObject(B);
         文字= f_obj.getString(值);     }     对于(INT C = 0; C< l.length(); C ++){
         JSONObject的l_obj = l.getJSONObject(C);         文字= l_obj.getString(值);
     }     JSONArray jArray1 = question.getJSONArray(evidencelist);     对于(INT D = 0; D< jArray1。长度(),D ++)
     {
        JSONObject的object3 = jArray1.getJSONObject(D);
        文字= object3.getString(文字);
     }
}

I'm receiving server response as JSON format, and need to get only field "text" and store it in arraylist. I tried but am getting only first text field as show below output, but i need all the text fields in evidencelist json object.Can anyone help me here to get all the text fields ?

Android code:

 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 output from server:

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.
解决方案

try this..

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

这篇关于如何获得只&QUOT;文字&QUOT;从JSON服务器响应申请?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 01:57