获取另一个的JSONObject内一个JSONObject参数 [英] obtain parameters of a JsonObject within another JsonObject

查看:204
本文介绍了获取另一个的JSONObject内一个JSONObject参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试获取图像的URL,这是一个JSONObject内,双方还内的其他的JSONObject

这是抛出我的错误

  3月5日至25日:09:51.120 9573-9836 / com.learn2crack.tab W / System.err的:org.json.JSONException:为thumbnail_images没有价值
3月5日至25日:09:51.134 9573-9836 / com.learn2crack.tab W / System.err的:在org.json.JSONObject.get(JSONObject.java:355)
3月5日至25日:09:51.134 9573-9836 / com.learn2crack.tab W / System.err的:在org.json.JSONObject.getJSONObject(JSONObject.java:574)
3月5日至25日:09:51.134 9573-9836 / com.learn2crack.tab W / System.err的:在com.learn2crack.tab.ListaCat $ GetLocales.doInBackground(ListaCat.java:141)
3月5日至25日:09:51.134 9573-9836 / com.learn2crack.tab W / System.err的:在com.learn2crack.tab.ListaCat $ GetLocales.doInBackground(ListaCat.java:92)
3月5日至25日:09:51.134 9573-9836 / com.learn2crack.tab W / System.err的:在android.os.AsyncTask $ 2.call(AsyncTask.java:288)
3月5日至25日:09:51.134 9573-9836 / com.learn2crack.tab W / System.err的:在java.util.concurrent.FutureTask.run(FutureTask.java:237)
3月5日至25日:09:51.135 9573-9836 / com.learn2crack.tab W / System.err的:在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)
3月5日至25日:09:51.135 9573-9836 / com.learn2crack.tab W / System.err的:在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
3月5日至25日:09:51.135 9573-9836 / com.learn2crack.tab W / System.err的:在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:587)
3月5日至25日:09:51.135 9573-9836 / com.learn2crack.tab W / System.err的:在java.lang.Thread.run(Thread.java:841)

JSON

  {\"status\":\"ok\",\"count\":2,\"count_total\":2,\"pages\":1,\"posts\":[{\"id\":4121,\"type\":\"post\",\"slug\":\"comida-2\",\"url\":\"http:\\/\\/web\\/comida-2\\/\",\"status\":\"publish\",\"title\":\"comida 2,title_plain:饮食2,内容:,节选:,日期:2014年4月18日二十时12分33秒,修改:2014- 05-17 19:10:09\",\"categories\":[{\"id\":13,\"slug\":\"comida\",\"title\":\"comida\",\"description\":\"\",\"parent\":0,\"post_count\":2}],\"tags\":[],\"author\":{\"id\":1,\"slug\":\"newmesis\",\"name\":\"newmesis\",\"first_name\":\"Luis\",\"last_name\":\"Ruiz\",\"nickname\":\"newmesis\",\"url\":\"\",\"description\":\"Ingeniero恩告知\\ u00e1tica IP圣托马斯Chillan\"},\"comments\":[],\"attachments\":[],\"comment_count\":0,\"comment_status\":\"open\",\"custom_fields\":{\"correo\":[\"\"],\"telefono\":[\"0\"],\"sample_custom_field\":[\"\"],\"sample_textarea\":[\"\"],\"sample_image_field\":[\"\"],\"layout\":[\"\"],\"sidebar\":[\"\"],\"header_title_bar\":[\"\"],\"header_transparency\":[\"\"],\"footer\":[\"\"],\"restrict_roles\":[\"\"]}},{\"id\":4119,\"type\":\"post\",\"slug\":\"comida-1\",\"url\":\"http:\\/\\/web\\/comida-1\\/\",\"status\":\"publish\",\"title\":\"comida 1,title_plain:饮食1,内容:,节选:,日期:2014年4月18日20时12分08秒,修改:2014- 05-25 03:02:26\",\"categories\":[{\"id\":13,\"slug\":\"comida\",\"title\":\"comida\",\"description\":\"\",\"parent\":0,\"post_count\":2}],\"tags\":[],\"author\":{\"id\":1,\"slug\":\"newmesis\",\"name\":\"newmesis\",\"first_name\":\"Luis\",\"last_name\":\"Ruiz\",\"nickname\":\"newmesis\",\"url\":\"\",\"description\":\"Ingeniero恩告知\\ u00e1tica IP圣托马斯Chillan\"},\"comments\":[],\"attachments\":[],\"comment_count\":0,\"comment_status\":\"open\",\"thumbnail\":null,\"custom_fields\":{\"correo\":[\"\"],\"telefono\":[\"81801756\"],\"sample_custom_field\":[\"\"],\"sample_textarea\":[\"\"],\"sample_image_field\":[\"\"],\"layout\":[\"\"],\"sidebar\":[\"\"],\"header_title_bar\":[\"\"],\"header_transparency\":[\"\"],\"footer\":[\"\"],\"mapa\":[\"a:3:{s:7:\\\"address\\\";s:49:\\\"Volc\án帕里纳科塔1202,冰寒\\ u00e1n,Biob \\ u00edo, Chile\\\";s:3:\\\"lat\\\";s:11:\\\"-36.6204309\\\";s:3:\\\"lng\\\";s:11:\\\"-72.0796431\\\";}\"],\"horario\":[\"\"]},\"thumbnail_size\":\"post-thumbnail\",\"thumbnail_images\":{\"full\":{\"url\":\"http:\\/\\/web\\/wp-content\\/uploads\\/sites\\/6\\/2014\\/05\\/mcdonalds21.jpg\",\"width\":560,\"height\":387},\"thumbnail\":{\"url\":\"http:\\/\\/web\\/wp-content\\/uploads\\/sites\\/6\\/2014\\/05\\/mcdonalds21-80x80.jpg\",\"width\":80,\"height\":80},\"medium\":{\"url\":\"http:\\/\\/web\\/wp-content\\/uploads\\/sites\\/6\\/2014\\/05\\/mcdonalds21-300x300.jpg\",\"width\":300,\"height\":300},\"twentyfourteen-full-width\":{\"url\":\"http:\\/\\/web\\/wp-content\\/uploads\\/sites\\/6\\/2014\\/05\\/mcdonalds21.jpg\",\"width\":560,\"height\":387}}}],\"query\":{\"ignore_sticky_posts\":true,\"count\":\"10\",\"status\":\"published\"}}

我试图获取参数完全

code

 的for(int i = 0; I< Locales.length();我++){
                        JSONObject的C = Locales.getJSONObject(I)                        字符串ID = c.getString(TAG_ID);
                        字符串title = c.getString(TAG_title);                        JSONObject的手机= c.getJSONObject(TAG_CUSTOM);
                        JSONArray araycus = phone.getJSONArray(TAG_TELEFONO);                        字符串移动= araycus.getString(0);
                        JSONObject的图像= c.getJSONObject(TAG_THUM);
                        JSONObject的fullurl = images.getJSONObject(TAG_THUM_FULL);                        字符串缩略图= fullurl.getString(TAG_THUM_FULL_URL);                        HashMap的<字符串,字符串> LocalesMAP =新的HashMap<字符串,字符串>();                        LocalesMAP.put(TAG_ID,身份证);
                        LocalesMAP.put(TAG_title,职称);
                        LocalesMAP.put(TAG_TELEFONO,手机);
                        LocalesMAP.put(TAG_THUM_FULL,缩略图);
                        LocalesList.add(LocalesMAP);


解决方案

试试这个..

而不是这个..

 的JSONObject图像= c.getJSONObject(TAG_THUM);
JSONObject的fullurl = images.getJSONObject(TAG_THUM_FULL);
字符串缩略图= fullurl.getString(TAG_THUM_FULL_URL);

添加

 的JSONObject图像= NULL;
串缩略图=;如果(c.has(TAG_THUM)){
   照片= c.getJSONObject(TAG_THUM);
   JSONObject的fullurl = images.getJSONObject(TAG_THUM_FULL);
   缩略图= fullurl.getString(TAG_THUM_FULL_URL);
}

有没有 thumbnail_images 第一,只有在第二个项目,以便为thumbnail_images无值使用为检查是否可用

try to get the url of an image, this is inside a JsonObject and that both also within other JsonObject

this is the error that throws me

05-25 03:09:51.120    9573-9836/com.learn2crack.tab W/System.err﹕ org.json.JSONException: No value for thumbnail_images
05-25 03:09:51.134    9573-9836/com.learn2crack.tab W/System.err﹕ at org.json.JSONObject.get(JSONObject.java:355)
05-25 03:09:51.134    9573-9836/com.learn2crack.tab W/System.err﹕ at org.json.JSONObject.getJSONObject(JSONObject.java:574)
05-25 03:09:51.134    9573-9836/com.learn2crack.tab W/System.err﹕ at com.learn2crack.tab.ListaCat$GetLocales.doInBackground(ListaCat.java:141)
05-25 03:09:51.134    9573-9836/com.learn2crack.tab W/System.err﹕ at com.learn2crack.tab.ListaCat$GetLocales.doInBackground(ListaCat.java:92)
05-25 03:09:51.134    9573-9836/com.learn2crack.tab W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
05-25 03:09:51.134    9573-9836/com.learn2crack.tab W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-25 03:09:51.135    9573-9836/com.learn2crack.tab W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
05-25 03:09:51.135    9573-9836/com.learn2crack.tab W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-25 03:09:51.135    9573-9836/com.learn2crack.tab W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-25 03:09:51.135    9573-9836/com.learn2crack.tab W/System.err﹕ at java.lang.Thread.run(Thread.java:841)

json

  {"status":"ok","count":2,"count_total":2,"pages":1,"posts":[{"id":4121,"type":"post","slug":"comida-2","url":"http:\/\/web\/comida-2\/","status":"publish","title":"comida 2","title_plain":"comida 2","content":"","excerpt":"","date":"2014-04-18 20:12:33","modified":"2014-05-17 19:10:09","categories":[{"id":13,"slug":"comida","title":"comida","description":"","parent":0,"post_count":2}],"tags":[],"author":{"id":1,"slug":"newmesis","name":"newmesis","first_name":"Luis","last_name":"Ruiz","nickname":"newmesis","url":"","description":"Ingeniero en inform\u00e1tica IP Santo Tomas Chillan"},"comments":[],"attachments":[],"comment_count":0,"comment_status":"open","custom_fields":{"correo":[""],"telefono":["0"],"sample_custom_field":[""],"sample_textarea":[""],"sample_image_field":[""],"layout":[""],"sidebar":[""],"header_title_bar":[""],"header_transparency":[""],"footer":[""],"restrict_roles":[""]}},{"id":4119,"type":"post","slug":"comida-1","url":"http:\/\/web\/comida-1\/","status":"publish","title":"comida 1","title_plain":"comida 1","content":"","excerpt":"","date":"2014-04-18 20:12:08","modified":"2014-05-25 03:02:26","categories":[{"id":13,"slug":"comida","title":"comida","description":"","parent":0,"post_count":2}],"tags":[],"author":{"id":1,"slug":"newmesis","name":"newmesis","first_name":"Luis","last_name":"Ruiz","nickname":"newmesis","url":"","description":"Ingeniero en inform\u00e1tica IP Santo Tomas Chillan"},"comments":[],"attachments":[],"comment_count":0,"comment_status":"open","thumbnail":null,"custom_fields":{"correo":[""],"telefono":["81801756"],"sample_custom_field":[""],"sample_textarea":[""],"sample_image_field":[""],"layout":[""],"sidebar":[""],"header_title_bar":[""],"header_transparency":[""],"footer":[""],"mapa":["a:3:{s:7:\"address\";s:49:\"Volc\u00e1n Parinacota 1202, Chill\u00e1n, Biob\u00edo, Chile\";s:3:\"lat\";s:11:\"-36.6204309\";s:3:\"lng\";s:11:\"-72.0796431\";}"],"horario":[""]},"thumbnail_size":"post-thumbnail","thumbnail_images":{"full":{"url":"http:\/\/web\/wp-content\/uploads\/sites\/6\/2014\/05\/mcdonalds21.jpg","width":560,"height":387},"thumbnail":{"url":"http:\/\/web\/wp-content\/uploads\/sites\/6\/2014\/05\/mcdonalds21-80x80.jpg","width":80,"height":80},"medium":{"url":"http:\/\/web\/wp-content\/uploads\/sites\/6\/2014\/05\/mcdonalds21-300x300.jpg","width":300,"height":300},"twentyfourteen-full-width":{"url":"http:\/\/web\/wp-content\/uploads\/sites\/6\/2014\/05\/mcdonalds21.jpg","width":560,"height":387}}}],"query":{"ignore_sticky_posts":true,"count":"10","status":"published"}}

I'm trying to get the parameter full

code

for (int i = 0; i < Locales.length(); i++) {
                        JSONObject c = Locales.getJSONObject(i);

                        String id = c.getString(TAG_ID);
                        String title = c.getString(TAG_title);

                        JSONObject phone = c.getJSONObject(TAG_CUSTOM);
                        JSONArray araycus = phone.getJSONArray(TAG_TELEFONO);

                        String mobile = araycus.getString(0);


                        JSONObject images = c.getJSONObject(TAG_THUM);
                        JSONObject fullurl = images.getJSONObject(TAG_THUM_FULL);

                        String thumbnail = fullurl.getString(TAG_THUM_FULL_URL);





                        HashMap<String, String> LocalesMAP = new HashMap<String, String>();

                        LocalesMAP.put(TAG_ID, id);
                        LocalesMAP.put(TAG_title, title);
                        LocalesMAP.put(TAG_TELEFONO, mobile);
                        LocalesMAP.put(TAG_THUM_FULL,thumbnail);


                        LocalesList.add(LocalesMAP);

解决方案

Try this..

Instead of this..

JSONObject images = c.getJSONObject(TAG_THUM);
JSONObject fullurl = images.getJSONObject(TAG_THUM_FULL);
String thumbnail = fullurl.getString(TAG_THUM_FULL_URL);

add

JSONObject images = null;
String thumbnail = "";

if(c.has(TAG_THUM)){
   images = c.getJSONObject(TAG_THUM);
   JSONObject fullurl = images.getJSONObject(TAG_THUM_FULL);
   thumbnail = fullurl.getString(TAG_THUM_FULL_URL);
}

There is no thumbnail_images in first that only in second item so that No value for thumbnail_images use has for checking is available or not

这篇关于获取另一个的JSONObject内一个JSONObject参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆