在android中解码JSON

时间:2012-11-12 13:30:39

标签: android json

我从JSON获取解码字符串时遇到问题。我与Web服务连接并获取JSON:{"pdf":"JVBERi0xLjQKJeLjz9MKNyAwIG9iaiAKPDwKL0ZpbHRlciAvRmxhdGVE... 这是字符串,但在Base64中编码。我尝试使用以下方法解码:

                JSONObject jsonMap = new JSONObject(out.toString());
                String json =jsonMap.getJSONObject("pdf").toString();
                byte temp[];
                Base64 b = null;

                temp = b.decode(json.getBytes(), 0);

但此解决方案不起作用。有任何想法吗? 这是错误日志:

11-12 14:36:30.257: W/System.err(12374): org.json.JSONException: Value JVBERi0xLjQKJeLjz9MKNyAwIG9iaiAKPDwKL0ZpbHRlciAvRmxhdGVEZWNvZGUKL0xlbmd0aCAyNTMKPj4Kc3RyZWFtCnicXZDLbsMgEEX3fAXLZBHhR+JsLKQoUSQv+lDdfgCGsYtUA8J44b/vAGkrdQE6w8yd4Q67drfO6MBevZU9BDpqozwsdvUS6ACTNqSsqNIyPKJ0y1k4wlDbb0uAuTOjbVvC3jC3BL/R3UXZAfaEvXgFXpuJ7j6uPcb96twXzGACLQjnVMGIfZ6EexYzsKQ6dArTOmwHlPwVvG8OaJXiMn9FWgWLExK8MBOQtig4be93TsCof7lzVgyj/BQeK0usLIq65MhV4qaOXOf3KvIxc3o/ZT5GbjKfIp8zN2nmo3ucHtfz44rK1Xt0lHaYrEQT2sDvmp11UZXON+CCezgKZW5kc3RyZWFtIAplbmRvYmogCjkgMCBvYmogCjw8Ci9GaWx0ZXIgL0ZsYXRlRGVjb2RlCi9MZW5ndGggOTYyMQovTGVuZ3RoMSAxNzQ1Mgo+PgpzdHJlYW0KeJzte3tglMW1+JmZb9/Z7GbzfpD9djcJIZsXmwQSHsnmKRAWAgRNkEhCsiHBkIQ8QHwBKlWD1rQ+QVvwbdWWzQZxAZFc8V0VrG2lSiFq2vqKUC/YW4XsPTO7BLD0Yn/3j/vHr/tx5pyZc+a85szMt0no6ep1QwhsAAbOxlUNnbEKnRoA3gIgp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Lzix7rYfWc87QWU7kYs7ZcJYzPr3ehpW8U3zrifSoU8b/GYxR4eUt0zwk6n9guwP8ykW2ygVLauTyvvpgbiurL
11-12 14:36:30.257: W/System.err(12374):    at org.json.JSON.typeMismatch(JSON.java:100)
11-12 14:36:30.257: W/System.err(12374):    at org.json.JSONObject.getJSONObject(JSONObject.java:573)
11-12 14:36:30.257: W/System.err(12374):    at pdf2app.models.Magazine.<init>(Magazine.java:74)
11-12 14:36:30.257: W/System.err(12374):    at pdf2app.activities.DownloaderService.parsePdfs(DownloaderService.java:490)
11-12 14:36:30.257: W/System.err(12374):    at pdf2app.activities.DownloaderService.getPDFs(DownloaderService.java:416)
11-12 14:36:30.257: W/System.err(12374):    at pdf2app.activities.DownloaderService.onHandleIntent(DownloaderService.java:183)
11-12 14:36:30.257: W/System.err(12374):    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
11-12 14:36:30.257: W/System.err(12374):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-12 14:36:30.257: W/System.err(12374):    at android.os.Looper.loop(Looper.java:137)

这是out.toString():

11-12 14:27:37.027: V/Loggggg(12308): {"pdf":"JVBERi0xLjQKJeLjz9MKNyAwIG9iaiAKPDwKL0ZpbHRlciAvRmxhdGVEZWNvZGUKL0xlbmd0aCAyNTMKPj4Kc3RyZWFtCnicXZDLbsMgEEX3fAXLZBHhR+JsLKQoUSQv+lDdfgCGsYtUA8J44b\/vAGkrdQE6w8yd4Q67drfO6MBevZU9BDpqozwsdvUS6ACTNqSsqNIyPKJ0y1k4wlDbb0uAuTOjbVvC3jC3BL\/R3UXZAfaEvXgFXpuJ7j6uPcb96twXzGACLQjnVMGIfZ6EexYzsKQ6dArTOmwHlPwVvG8OaJXiMn9FWgWLExK8MBOQtig4be93TsCof7lzVgyj\/BQeK0usLIq65MhV4iZxnd+ryMfMdeRT5mPkJvMp8jlzk2Y+usfpcT0\/rqhcvUdHaYfJSjShDfyu2VkXVel8A990ezYKZW5kc3RyZWFtIApl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DQ3Yp314L6y6U23jY0oA0cS1Oqm4aq0fV9mMSaRQp6o5sb6r1kM7pU+Ez4rILza7NXcU7TSsWrtZfbO4ZWNuHSxA15YeF6my8uzr0\/cBLiqpShunq7zVsab29orkwYjoKhhetHJrmVSVdKMjOGTRHBxA6HG0NEmOFyom1CJiihzqmahROZJTwi+2wsCK\/SomAk9XacUwFv2gpgqKUA1fDTQHCUtxVXpNOrrWgaMhVxPh\/vVaWY7MrQOcAKsI99eiWnOcRRp5jOASd5nUyUGsov0l6Hw5uezktEU4FrijGWiH5eZsZaP91p7zUpiDB9UIu5bW4oysb022x8gbf43bAcO96NC+qDfQWWx\/vAne1o8NImLhm9KIlezCUbL0omhjfZsZL3im890

看起来问题符合问题:String json =jsonMap.getJSONObject("pdf").toString();因为在该行之后的调试器中我收到错误

2 个答案:

答案 0 :(得分:3)

看起来解析错误

            JSONObject jsonMap = new JSONObject(out.toString());
            String json =jsonMap.getString("pdf");

然后执行base64解码。

答案 1 :(得分:2)

而不是jsonMap.getJSONObject("pdf"),您应该使用jsonMap.getString("pdf")