Daniel
Daniel

Reputation: 1564

cannot convert json formated string to Java map

I have the following string that cannot be converted to 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
  }
}

My Code, where jsonstr is the string above

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

The only trick might be that jsonstr's value is received from android's Message class.

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

in sending side:

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

in receiving side:

    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)

I was suspecting transmitting the string may ruins its format. Is it possible?

Upvotes: 1

Views: 690

Answers (1)

fada21
fada21

Reputation: 3288

JSON is valid. I've tested your case and it worked. Mayby your encoding charset is broken when you generate string 'theoriginalstrcontainingjson' before sending message?

My test code (in the assets folder I included your json as '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;                                                                                          
    }                                                                                                         

}                                                                                                             

Upvotes: 1

Related Questions