无法将类型为java.lang.String的Value Bundle转换为JSONArray

时间:2016-07-02 07:43:43

标签: java android json parsing

以下是我对服务器的回复

    [
{
"updates":[
{
"id":6,
"name":"Food Express",
"photo":"http:\/\/www.abc.org\/development\/data\/restaurant\/1451480096.jpg",
"updates":"testing updatessss"
}
],
"count":0
},
{
"updates":[
{
"id":96,
"name":"Madras cafe",
"photo":"http:\/\/www.abc.org\/development\/img\/res-bg.jpg",
"updates":"asdfasdfsafasdfsa"
},
{
"id":96,
"name":"Madras cafe",
"photo":"http:\/\/www.abc.org\/development\/img\/res-bg.jpg",
"updates":"vcxvzxvzx"
}
],
"count":4
}
]

这是JSON响应我在Android中解析但导致错误..

这是我的安卓代码

  JSONArray jsonarray = new JSONArray(response);
       Log.e("Hello","Hi"+jsonarray);
         for (int i = 0; i < jsonarray.length(); i++) {
    JSONObject jsonObjectmain = jsonarray .getJSONObject(i);
        JSONArray array = jsonObjectmain.getJSONArray("updates");
        for (int j = 0; j < array.length(); j++) {
         JSONObject jsonObject = array.getJSONObject(j);
         String id = jsonObject.getString("id").toString();
         String name = jsonObject.getString("name").toString();
         String photo = jsonObject.getString("photo").toString();
         String updates = jsonObject.getString("updates").toString();

        }      
         }

我想把这个东西放在onMessage()方法的推送上,但是我无法这样做。

这是我的堆栈跟踪...

07-02 13:08:53.553: W/System.err(19037): org.json.JSONException: Value Bundle of type java.lang.String cannot be converted to JSONArray
07-02 13:08:53.553: W/System.err(19037):    at org.json.JSON.typeMismatch(JSON.java:111)
07-02 13:08:53.553: W/System.err(19037):    at org.json.JSONArray.<init>(JSONArray.java:96)
07-02 13:08:53.553: W/System.err(19037):    at org.json.JSONArray.<init>(JSONArray.java:108)
07-02 13:08:53.553: W/System.err(19037):    at com.abc.ff.GCMIntentService.parseList(GCMIntentService.java:117)
07-02 13:08:53.553: W/System.err(19037):    at com.abc.ff.GCMIntentService.onMessage(GCMIntentService.java:63)
07-02 13:08:53.553: W/System.err(19037):    at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:223)
07-02 13:08:53.553: W/System.err(19037):    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
07-02 13:08:53.553: W/System.err(19037):    at android.os.Handler.dispatchMessage(Handler.java:102)
07-02 13:08:53.553: W/System.err(19037):    at android.os.Looper.loop(Looper.java:149)
07-02 13:08:53.553: W/System.err(19037):    at android.os.HandlerThread.run(HandlerThread.java:61)

这是我的onMessage()方法......

@Override
    protected void onMessage(Context context, Intent data) {

        String message; 

         message = data.getExtras().getString("updates");

         Log.e("",""+data.getExtras().toString());

        Intent intent = new Intent(this, MyActivity.class);
        parseList(data.getExtras().toString());

        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        Notification notification = new Notification.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setWhen(System.currentTimeMillis())
                .setContentTitle("Title")
                .setContentText(message).setContentIntent(pIntent)
                .getNotification();

        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(R.string.app_name, notification);

        {

            PowerManager pm = (PowerManager) context
                    .getSystemService(Context.POWER_SERVICE);
            final PowerManager.WakeLock mWakelock = pm.newWakeLock(
                    PowerManager.FULL_WAKE_LOCK
                            | PowerManager.ACQUIRE_CAUSES_WAKEUP, "GCM_PUSH");
            mWakelock.acquire();
            notification.defaults|=Notification.DEFAULT_VIBRATE;


            Timer timer = new Timer();
            TimerTask task = new TimerTask() {
                public void run() {
                    mWakelock.release();
                }
            };
            timer.schedule(task, 5000);
        }

    }

2 个答案:

答案 0 :(得分:2)

我认为您在GCMIntentService中收到JsonObject,其JsonArray而不是Bundle。您应该将JsonObject转换为JSONObject json = new JSONObject(); Set<String> keys = bundle.keySet(); for (String key : keys) { try { json.put(key, bundle.get(key)); } catch (JSONException e) { e.printStackTrace(); } } ,如下所示:

JSONArray jsonarray = new JSONArray(json.getString("response"));
Log.e("Hello","Hi"+jsonarray);
for (int i = 0; i < jsonarray.length(); i++) {
    JSONObject jsonObjectmain = jsonarray .getJSONObject(i);
    JSONArray array = jsonObjectmain.getJSONArray("updates");
    for (int j = 0; j < array.length(); j++) {
         JSONObject jsonObject = array.getJSONObject(j);
         String id = jsonObject.getString("id").toString();
         String name = jsonObject.getString("name").toString();
         String photo = jsonObject.getString("photo").toString();
         String updates = jsonObject.getString("updates").toString();

    }      
}

然后你可以像这样进行正常处理:

parseList

<强>更新

String方法的参数类型从Bundle更改为parseList(Bundle bundle) ,因此您的parseList方法应如下所示:

parseList(data.getExtras().toString());

并将parseList(data.getExtras());替换为parseList(Bundle bundle)

现在在 let prdicate:NSPredicate = NSPredicate(format: "self.casino_name contains[c] %@", searchText) let filterArray = self.yourArray.filter({ prdicate.evaluateWithObject($0) }) print(filterArray) 方法中,您可以将包转换为json,如上所述。

答案 1 :(得分:1)

从服务器端的响应中的JSON开始删除Bundle[{response=

如果你想从代码而不是替换

 response.replace("Bundle[{response=","");