简体   繁体   中英

getting HTTP status 406 Error while converting Json String to JSONObject

How do I convert the below string to JSON object. I tried the below line of code

{"v_root_node_name":"rparama","v_node_name":"The","v_root_node_id":"given","v_entityname":"callerid","v_fullname":"is","v_managedby":"not","v_exch_sync":"valid."}

Code :

    JSONObject jsonObj = "{"v_root_node_name":"rparama","v_node_name":"The","v_root_node_id":"given","v_entityname":"callerid","v_fullname":"is","v_managedby":"not","v_exch_sync":"valid."}";
     try {
         jsonObj = new JSONObject(jsonInString);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

I get HTTP 406 Error . WHat mistake did I do in my code. Can you please let me know Thanks in advance.

I am not sure if you are trying to initialice a new JSONObject out of your String. If you are, try this:

String jsonString = "{\"v_root_node_name\":\"rparama\",\"v_node_name\":\"The\",\"v_root_node_id\":\"given\",\"v_entityname\":\"callerid\",\"v_fullname\":\"is\",\"v_managedby\":\"not\",\"v_exch_sync\":\"valid.\"}";

JSONObject json = (JSONObject) new JSONParser().parse(jsonString);

Please see if the json is correctly formed. Use an online editor or simple use Object mapper. Please refer the example below.

ObjectMapper objectMapper = new ObjectMapper();

String carJson =
    "{ \"brand\" : \"Mercedes\", \"doors\" : 5 }";


try {

    Car car = objectMapper.readValue(carJson, Car.class);

    System.out.println("car.brand = " + car.brand);
    System.out.println("car.doors = " + car.doors);
} catch (IOException e) {
    e.printStackTrace();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM