1

我有以下字符串无法转换为 java map

{
  "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)

例外:

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)

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

4

1 回答 1

1

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

我的测试代码(在资产文件夹中,我将您的 json 包含为“test.json”): package 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;                                                                                          
    }                                                                                                         

}                                                                                                             
于 2013-11-24T01:00:05.323 回答