无法在android中获取JSONObject的值

时间:2014-04-14 10:46:58

标签: android json arrays jsonobject

我无法获得" wpcf-video"的价值。和" wpcf-icon"来自" custom_fields"来自我的JSON网址的对象。我想要的是获得价值,如果每个" wpcf"在自定义字段中,并将其作为字符串,以便我可以" putExtra"它的值和getIntent它在下一个活动中。请帮忙。我是android的初学者,如果我得到很多直接的解决方案,它会非常有用,我在我的大学里为我的论文做这个。感谢您的理解..

JSON网址:

    "status": "ok",
    "count": 3,
    "count_total": 3,
    "pages": 1,
    "posts": [
   {
  "id": 85,
  "title": "Lesson 3 commarts",
  "content": "<p>Lesson 3 commarts content here<\/p>\n",
  "excerpt": "<p>Lesson 3 commarts content here<\/p>\n",
  "custom_fields": {
    "wpcf-video": [
      ""
    ],
    "wpcf-pdf": [
      ""
    ],
    "wpcf-icon": [
      "http:\/\/loaapp.abundantlife.org.au\/wp-content\/uploads\/2014\/04\/icon2.png"
    ]
  }
},
{
  "id": 81,
  "title": "Lesson 2 Com Arts 1",
  "content": "<p>Lesson 2 Com Arts 1 Content Here<\/p>\n",
  "excerpt": "<p>Lesson 2 Com Arts 1 Content Here<\/p>\n",
  "custom_fields": {
    "wpcf-video": [
      ""
    ],
    "wpcf-pdf": [
      "http:\/\/loaapp.abundantlife.org.au\/wp-content\/uploads\/2014\/04\/single-course-layout.docx"
    ],
    "wpcf-icon": [
      "http:\/\/loaapp.abundantlife.org.au\/wp-content\/uploads\/2014\/04\/iconmenu.png"
    ]
  }
}

我的代码:

    try {

                HttpClient client = new DefaultHttpClient();
                HttpPost post = new HttpPost(params[0]);
                HttpResponse response = client.execute(post);

                int status = response.getStatusLine().getStatusCode();

                if(status == 200){

                    HttpEntity entity = response.getEntity();
                    String data = EntityUtils.toString(entity);

                    JSONObject lessonObject = new JSONObject(data);
                    JSONArray lessonArray = lessonObject.getJSONArray("posts");

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

                        JSONObject objectLesson = lessonArray.getJSONObject(i);

                        LessonArrayTemplate LessonTemplateArrayList = new LessonArrayTemplate();

                        LessonTemplateArrayList.setTitle(objectLesson.getString("title"));
                        LessonTemplateArrayList.setExcerpt(objectLesson.getString("excerpt").replaceAll("<(.*?)\\>","").replaceFirst("(.*?)\\>", "").replaceAll("&rarr;","").replace("Continue reading", ""));
                        LessonTemplateArrayList.setId(objectLesson.getString("id"));

                        JSONObject customFieldsObject = objectLesson.getJSONObject("custom_fields");

                        //fetching the url of the video
                        LessonTemplateArrayList.setVideo(customFieldsObject.getString("wpcf-video").replace("\\", ""));
                        LessonTemplateArrayList.setIconImage(customFieldsObject.getString("wpcf-icon").replace("\\", ""));


                        // sample input but this should be the image 

                        comArtsArrayList.add(LessonTemplateArrayList);  
                    }

3 个答案:

答案 0 :(得分:0)

试试这段代码

           try {

                    HttpClient httpclient2 = new DefaultHttpClient();
                    HttpPost httppost2 = new HttpPost("http://10.0.2.2:80/contactcheck.php");

                    HttpResponse response2 = httpclient2.execute(httppost2); 
                    HttpEntity entity2 = response2.getEntity();
                    is2 = entity2.getContent();

                    Log.e("log_tag", "connection success ");

            }
                catch(Exception e)
                {
                        Log.e("log_tag", "Error in http connection "+e.toString());


                }

                try
                {
                        BufferedReader reader2 = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                        StringBuilder sb2 = new StringBuilder();
                        String line = null;
                        while ((line = reader2.readLine()) != null) 
                        {
                                sb2.append(line + "\n");

                        }
                        is.close();

                        result2=sb2.toString();
                }
                catch(Exception e)
                {
                       Log.e("log_tag", "Error converting result "+e.toString());


                }
                try
                {
                    JSONArray jArray2 = new JSONArray(result3);
                    String s11;
                    Log.w("Lengh",""+jArray2.length());
                      for(int i=0;i<jArray2.length();i++){


                        JSONObject json_data2 = jArray2.getJSONObject(i);

                           s11=json_data2.getString("phno");





                      }  
                }

                catch(JSONException e)
                {
                        Log.e("log_tag", "Error parsing data "+e.toString());

                }

答案 1 :(得分:0)

试试这个

  JSONArray videoArray =  customFieldsObject.getJSONArray("wpcf-video")  ;
  JSONArray iconArray =  customFieldsObject.getJSONArray("wpcf-icon");
  if(videoArray.length()>0){
             LessonTemplateArrayList.setVideo(videoArray.get(0));
}
  if(iconArray.length()>0){
            LessonTemplateArrayList.setIconImage(iconArray.get(0));
}

而不是

  customFieldsObject.getString("wpcf-video").replace("\\", "");
  customFieldsObject.getString("wpcf-icon").replace("\\", "");

wpcf-videowpcf-icon是jsonArray,所以将它们作为JSONArray

答案 2 :(得分:0)

这样做

替换这两行

LessonTemplateArrayList.setVideo(customFieldsObject.getString("wpcf-video").replace("\\", ""));
LessonTemplateArrayList.setIconImage(customFieldsObject.getString("wpcf-icon").replace("\\", ""));

使用

LessonTemplateArrayList.setVideo(customFieldsObject.getJSONArray("wpcf-video").getString(0));
    LessonTemplateArrayList.setIconImage(customFieldsObject.getJSONArray("wpcf-icon").getString(0));
相关问题