从字符串创建新的JSONObject返回Null-JAVA/Android [英] Creating new JSONObject from String Returns Null - JAVA / Android

查看:79
本文介绍了从字符串创建新的JSONObject返回Null-JAVA/Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从字符串创建新的JSONObject,但是由于某种原因,新的JSONObject始终为null-我不确定为什么.

I'm attempting to create a new JSONObject from a String however for some reason the new JSONObject is always null - and I'm unsure why.

任何建议都值得赞赏.

Any suggestions are appreciated.

JSONObject messagesObj;


    String mArr = intent.getStringExtra("msgArr");

            try {
                if (mArr != null)
                    messagesObj = new JSONObject(mArr);

                if (messagesObj != null)
                    populateMessages(messagesObj);
                DataManager.clientChatMarkMessagesSeen(chatId);
            } catch (JSONException e) {
            //    DataManager.clientChatLoad(this);
                e.printStackTrace();
            }

值:

String mArr = [{"message":"User has joined the chat.","type":"agent","created":"2016-12-07 17:35:09","name":"User"},{"message":"Hello World?","type":"agent","created":"2016-12-07 17:35:17","name":"User"},{"message":"User has left the chat.","type":"agent","created":"2016-12-07 17:38:40","name":"User"}]

推荐答案

因为它不是JSON对象,而是JSONArray.尝试创建JSONArray而不是JSONObject

Because that isn't a json object- its a JSONArray. Try creating a JSONArray instead of a JSONObject

这篇关于从字符串创建新的JSONObject返回Null-JAVA/Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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