gpt4 book ai didi

android - 无法将 json 格式的字符串转换为 Java 映射

转载 作者:行者123 更新时间:2023-11-30 03:15:35 25 4
gpt4 key购买 nike

我有以下无法转换为 java 映射的字符串

{
"msg_id" : "6b0af820-6bf8-4bc8-823e-a8f7435b69da",
"msg_body" : "path from chongqing to shanghai",
"outcome" : {
"intent" : "route_",
"entities" : {
"location" : {
"end" : 19,
"start" : 10,
"value" : "chongqing",
"body" : "chongqing",
"suggested" : true
},
"destination" : {
"end" : 31,
"start" : 23,
"value" : "shanghai",
"body" : "shanghai",
"suggested" : true
}
},
"confidence" : 0.682
}
}

我的代码,其中jsonstr就是上面的字符串

JSONObject jas= new JSONObject(jsonstr); //This is where Exception thrown

唯一的技巧可能是 jsonstr 的值是从 android 的 Message 类接收的。

    import android.os.Message;
import android.os.Messenger;
import org.json.JSONObject;

在发送端:

    Message messageToClient = Message.obtain(null, 0, theoriginalstrcontainingjson);
client.send(messageToClient);

在接收方:

    void handleMessage(Message msg){
jsonstr = msg.obj.toString();
JSONObject jas= new JSONObject(jsonstr); (exception happens)

异常(exception):

11-24 07:45:15.473: W/System.err(12674): org.json.JSONException: Value get of type java.lang.String cannot be converted to JSONObject
11-24 07:45:15.473: W/System.err(12674): at org.json.JSON.typeMismatch(JSON.java:111)
11-24 07:45:15.473: W/System.err(12674): at org.json.JSONObject.<init>(JSONObject.java:158)
11-24 07:45:15.473: W/System.err(12674): at org.json.JSONObject.<init>(JSONObject.java:171)

我怀疑传输字符串可能会破坏其格式。可能吗?

最佳答案

JSON 有效。我已经测试了你的案例并且它有效。当您在发送消息之前生成字符串 'theoriginalstrcontainingjson' 时,您的编码字符集可能被破坏了吗?

我的测试代码(在 Assets 文件夹中,我将您的 json 作为“test.json”包含在内): 包 com.fada21.switchplayground;

import java.io.BufferedReader;                                                                                
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Handler handler = new Handler() {

@Override
public void handleMessage(Message msg) {
Log.d("", msg.obj.toString());
try {
new JSONObject(msg.obj.toString());
Log.d("", "Success!");
} catch (JSONException e) {
Log.d("", "Fail!", e);
}
}
};

String s = decodeJsonAsset();
Message message = Message.obtain(null, 0, s);
Messenger client = new Messenger(handler);

try {
client.send(message);
} catch (RemoteException e) {
e.printStackTrace();
}

}

private String decodeJsonAsset() {
try {
InputStream is = getResources().getAssets().open("test.json");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String s;
while ((s = reader.readLine()) != null) {
sb.append(s);
}
is.close();
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

关于android - 无法将 json 格式的字符串转换为 Java 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20169441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com