2013-11-23 61 views
1

我有以下字符串不能轉換爲Java地圖不能JSON格式化字符串轉換成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) 

例外:

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) 

我被懷疑發送所述串可以廢墟其格式。可能嗎?

回答

1

JSON有效。我已經測試過你的案例,它的工作。在發送消息之前生成字符串'theoriginalstrcontainingjson'時,Mayby編碼字符集是否被破壞?我的測試代碼(在資產文件夾中,我包括你的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;                       
    }                           

}                            
+0

傳輸發生在兩個活動之間,如果它很重要的話。我在發送活動中進行了測試,並且發揮了作用,但在接收方中卻沒有 – Daniel