【发布时间】:2018-01-03 20:34:47
【问题描述】:
我一直在尝试在 android 中转换 JSON。我从我的 socket.io-server 收到这个作为响应:
[{
"_id":"5a4cee2808699d0014d99dfc",
"conversation_id":"5a4cee2808699d0014d99dfb",
"message":"text1",
"author":"5a173c64187e5c335488e906",
"created_at":"1514991144450",
"__v":0
},{
"_id":"5a4cee4208699d0014d99dfd",
"conversation_id":"5a4cee2808699d0014d99dfb",
"message":"text2",
"author":"5a2505c50c1ff300149d799f",
"created_at":"1514991170944",
"__v":0
},{
"_id":"5a4cee8608699d0014d99dfe",
"conversation_id":"5a4cee2808699d0014d99dfb",
"message":"text3",
"author":"5a173c64187e5c335488e906",
"created_at":"1514991238321",
"__v":0
},{
"_id":"5a4ceea908699d0014d99dff",
"conversation_id":"5a4cee2808699d0014d99dfb",
"message":"text4",
"author":"5a2505c50c1ff300149d799f",
"created_at":"1514991273472",
"__v":0
},{
"_id":"5a4ceed508699d0014d99e00",
"conversation_id":"5a4cee2808699d0014d99dfb",
"message":"text5",
"author":"5a173c64187e5c335488e906",
"created_at":"1514991317353",
"__v":0
},{
"_id":"5a4ceef108699d0014d99e01",
"conversation_id":"5a4cee2808699d0014d99dfb",
"message":"text6;)",
"author":"5a2505c50c1ff300149d799f",
"created_at":"1514991345153",
"__v":0
},{
"_id":"5a4cef1c08699d0014d99e02",
"conversation_id":"5a4cee2808699d0014d99dfb",
"message":"text7",
"author":"5a173c64187e5c335488e906",
"created_at":"1514991388937",
"__v":0
}]
通常人们会尝试从 socket.io 网站获取这样的 JSONObject:
private Emitter.Listener onNewMessage = new Emitter.Listener() {
@Override
public void call(final Object.. args) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
JSONObject data = (JSONObject) args[0];
String username;
String message;
try {
username = data.getString("username");
message = data.getString("message");
} catch (JSONException e) {
return;
}
// add the message to view
addMessage(username, message);
}
});
}
但问题是这是针对一个 JSON 对象的,所以由于我的数据是一个数组,所以这不需要另一种方法吗?
我一直在尝试按照网站所说的传统方式进行操作,但它只是返回:
java.lang.ClassCastException: java.lang.String 无法转换为 org.json.JSONObject
提前感谢您尝试帮助我,我是这种东西的新手!
编辑:
private Emitter.Listener onConversationJoined = new Emitter.Listener() {
@Override
public void call(final Object... args) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
String data = args[0].toString();
Log.e("TestIt", data);
JSONArray jsonarray;
try {
jsonarray = new JSONArray(args[0]);
for (int i=0; i < jsonarray.length(); i++) {
JSONObject chatmsg = jsonarray.getJSONObject(i);
String conversation_id = chatmsg.getString("conversation_id");
String message = chatmsg.getString("message");
String author = chatmsg.getString("author");
String created_at = chatmsg.getString("created_at");
addMessageUser(message);
// Whatever you want to do with these fields.
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
};
它在 LogCat 中说:
01-03 22:04:53.758 32259-32259/com.app.app.app W/System.err: org.json.JSONException: 不是原始数组:类 java.lang.String
在这一行:
jsonarray = new JSONArray(args[0]);
【问题讨论】:
-
args[0] 的值是多少?
-
您的第二个代码看起来更有希望,但您应该测试您的 args[0] 参数以查看它包含的内容:
String data = args[0].toString(); Log.e("TestIt", data);。字符串看起来像你期望的那样吗?? -
您好,当我记录 args[0].toString() 时,它给了我想要的字符串,如问题中所示...它仍然给我 > org.json.JSONException: Not a original array : class java.lang.String,我已经用我现在的代码编辑了这个问题。
标签: android json node.js socket.io