Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSONObject.put string method adding extra \ character to JSON String

Tags:

json

android

I have a json string which has pid, name, price as elements. I'm displaying it in logcat. But this string changes as i use the put method to add a json string to json object. When i print the json object '\' charcater is added to every key value pair. I'm not able to figure out what is actually causing the problem.

json is a plain string and jObj is JSONObject.

My logcat details are:

json = {"products":[{"pid":"14","name":"zxc","price":"123456"},{"pid":"6","name":"Sony Xperia","price":"35000"},{"pid":"8","name":"Samsung Galaxy Note","price":"32000"},{"pid":"5","name":"htc","price":"26326"},{"pid":"9","name":"Nokia Lumia 800","price":"18000"},{"pid":"2","name":"iphone","price":"12345"},{"pid":"15","name":"sdjnas","price":"12243"},{"pid":"13","name":"Samsung S5222","price":"6500"},{"pid":"11","name":"Nokia C201","price":"4400"},{"pid":"7","name":"Nokia Asha 200","price":"4000"},{"pid":"1","name":"htc","price":"1234"},{"pid":"3","name":"htc","price":"1234"},{"pid":"4","name":"htc","price":"1234"},{"pid":"10","name":"aks","price":"1234"},{"pid":"12","name":"asd","price":"123"}],"success":1}

After writing the following line of code :

jObj.put("details", json);

An extra character '\' is added.

json object = {"details":"{\"products\":[{\"pid\":\"14\",\"name\":\"zxc\",\"price\":\"123456\"},{\"pid\":\"6\",\"name\":\"Sony Xperia\",\"price\":\"35000\"},{\"pid\":\"8\",\"name\":\"Samsung Galaxy Note\",\"price\":\"32000\"},{\"pid\":\"5\",\"name\":\"htc\",\"price\":\"26326\"},{\"pid\":\"9\",\"name\":\"Nokia Lumia 800\",\"price\":\"18000\"},{\"pid\":\"2\",\"name\":\"iphone\",\"price\":\"12345\"},{\"pid\":\"15\",\"name\":\"sdjnas\",\"price\":\"12243\"},{\"pid\":\"13\",\"name\":\"Samsung S5222\",\"price\":\"6500\"},{\"pid\":\"11\",\"name\":\"Nokia C201\",\"price\":\"4400\"},{\"pid\":\"7\",\"name\":\"Nokia Asha 200\",\"price\":\"4000\"},{\"pid\":\"1\",\"name\":\"htc\",\"price\":\"1234\"},{\"pid\":\"3\",\"name\":\"htc\",\"price\":\"1234\"},{\"pid\":\"4\",\"name\":\"htc\",\"price\":\"1234\"},{\"pid\":\"10\",\"name\":\"aks\",\"price\":\"1234\"},{\"pid\":\"12\",\"name\":\"asd\",\"price\":\"123\"}],\"success\":1}\n"}

Addition of backslashes is causiing problem in getting the string. JSONException no value for success is shown in logcat :

JSONObject json = null;

List<NameValuePair> params = new ArrayList<NameValuePair>();
        JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);

        Log.d("All Products: ", json.toString());

        try
        {
            int success = json.getInt("success");

            if (success == 1)
            {
                products = json.getJSONArray(TAG_PRODUCTS);

                for (int i = 0; i < products.length(); i++)
                {
                    JSONObject c = products.getJSONObject(i);

                    String id = c.getString(TAG_PID);
                    String name = c.getString(TAG_NAME);

                    HashMap<String, String> map = new HashMap<String, String>();

                    map.put(TAG_PID, id);
                    map.put(TAG_NAME, name);

                    productsList.add(map);
                }
            }
            else
            {
                Intent i = new Intent(getApplicationContext(), NewProductActivity.class);
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
            }
        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }

The logcat is :

05-29 15:54:21.772: W/ThrottleService(91): unable to find stats for iface rmnet0
05-29 15:54:26.192: I/ActivityManager(91): START {cmp=com.example.androidhive/.AllProductsActivity} from pid 588
05-29 15:54:26.192: W/WindowManager(91): Failure taking screenshot for (180x300) to layer 21010
05-29 15:54:26.502: I/WindowManager(91): createSurface Window{415a5d70 com.example.androidhive/com.example.androidhive.AllProductsActivity paused=false}: DRAW NOW PENDING
05-29 15:54:26.543: I/WindowManager(91): createSurface Window{415b8c88     com.example.androidhive/com.example.androidhive.AllProductsActivity paused=false}: DRAW NOW     PENDING
05-29 15:54:27.002: I/ARMAssembler(35): generated scanline__00000077:03010104_00008001_00000000 [ 89 ipp] (110 ins) at [0x41e00670:0x41e00828] in 1129891 ns
05-29 15:54:27.202: I/ActivityManager(91): Displayed com.example.androidhive/.AllProductsActivity: +980ms
05-29 15:54:27.472: D/dalvikvm(588): GC_CONCURRENT freed 125K, 3% free 10212K/10439K, paused 5ms+4ms
05-29 15:54:27.853: I/JSONPArser.java(588): json = {"products":    [{"pid":"14","name":"zxc","price":"123456"},{"pid":"6","name":"Sony Xperia","price":"35000"},{"pid":"8","name":"Samsung Galaxy Note","price":"32000"},{"pid":"5","name":"htc","price":"26326"},{"pid":"9","name":"Nokia Lumia 800","price":"18000"},{"pid":"2","name":"iphone","price":"12345"},{"pid":"15","name":"sdjnas","price":"12243"},{"pid":"13","name":"Samsung S5222","price":"6500"},{"pid":"11","name":"Nokia C201","price":"4400"},{"pid":"7","name":"Nokia Asha 200","price":"4000"},{"pid":"1","name":"htc","price":"1234"},{"pid":"3","name":"htc","price":"1234"},{"pid":"4","name":"htc","price":"1234"},{"pid":"10","name":"aks","price":"1234"},{"pid":"12","name":"asd","price":"123"}],"success":1}
05-29 15:54:27.892: I/JSONPArser.java(588): json object = {"details":"{\"products\":[{\"pid\":\"14\",\"name\":\"zxc\",\"price\":\"123456\"},{\"pid\":\"6\",\"name\":\"Sony Xperia\",\"price\":\"35000\"},{\"pid\":\"8\",\"name\":\"Samsung Galaxy Note\",\"price\":\"32000\"},{\"pid\":\"5\",\"name\":\"htc\",\"price\":\"26326\"},{\"pid\":\"9\",\"name\":\"Nokia Lumia 800\",\"price\":\"18000\"},{\"pid\":\"2\",\"name\":\"iphone\",\"price\":\"12345\"},{\"pid\":\"15\",\"name\":\"sdjnas\",\"price\":\"12243\"},{\"pid\":\"13\",\"name\":\"Samsung S5222\",\"price\":\"6500\"},{\"pid\":\"11\",\"name\":\"Nokia C201\",\"price\":\"4400\"},{\"pid\":\"7\",\"name\":\"Nokia Asha 200\",\"price\":\"4000\"},{\"pid\":\"1\",\"name\":\"htc\",\"price\":\"1234\"},{\"pid\":\"3\",\"name\":\"htc\",\"price\":\"1234\"},{\"pid\":\"4\",\"name\":\"htc\",\"price\":\"1234\"},{\"pid\":\"10\",\"name\":\"aks\",\"price\":\"1234\"},{\"pid\":\"12\",\"name\":\"asd\",\"price\":\"123\"}],\"success\":1}\n"}
05-29 15:54:27.892: I/JSONPArser.java(588): Line 27
05-29 15:54:27.902: I/JSONPArser.java(588): Line 30
05-29 15:54:27.902: D/All Products:(588): {"details":"{\"products\":[{\"pid\":\"14\",\"name\":\"zxc\",\"price\":\"123456\"},{\"pid\":\"6\",\"name\":\"Sony Xperia\",\"price\":\"35000\"},{\"pid\":\"8\",\"name\":\"Samsung Galaxy Note\",\"price\":\"32000\"},{\"pid\":\"5\",\"name\":\"htc\",\"price\":\"26326\"},{\"pid\":\"9\",\"name\":\"Nokia Lumia 800\",\"price\":\"18000\"},{\"pid\":\"2\",\"name\":\"iphone\",\"price\":\"12345\"},{\"pid\":\"15\",\"name\":\"sdjnas\",\"price\":\"12243\"},{\"pid\":\"13\",\"name\":\"Samsung S5222\",\"price\":\"6500\"},{\"pid\":\"11\",\"name\":\"Nokia C201\",\"price\":\"4400\"},{\"pid\":\"7\",\"name\":\"Nokia Asha 200\",\"price\":\"4000\"},{\"pid\":\"1\",\"name\":\"htc\",\"price\":\"1234\"},{\"pid\":\"3\",\"name\":\"htc\",\"price\":\"1234\"},{\"pid\":\"4\",\"name\":\"htc\",\"price\":\"1234\"},{\"pid\":\"10\",\"name\":\"aks\",\"price\":\"1234\"},{\"pid\":\"12\",\"name\":\"asd\",\"price\":\"123\"}],\"success\":1}\n"}
05-29 15:54:27.933: W/System.err(588): org.json.JSONException: No value for success
05-29 15:54:27.933: W/System.err(588):  at org.json.JSONObject.get(JSONObject.java:354)
05-29 15:54:27.933: W/System.err(588):  at org.json.JSONObject.getInt(JSONObject.java:443)
05-29 15:54:27.942: W/System.err(588):  at com.example.androidhive.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:110)
05-29 15:54:27.942: W/System.err(588):  at com.example.androidhive.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:1)
05-29 15:54:27.973: W/System.err(588):  at android.os.AsyncTask$2.call(AsyncTask.java:264)
05-29 15:54:27.973: W/System.err(588):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-29 15:54:27.973: W/System.err(588):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-29 15:54:27.973: W/System.err(588):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
05-29 15:54:27.973: W/System.err(588):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-29 15:54:27.982: W/System.err(588):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-29 15:54:27.982: W/System.err(588):  at java.lang.Thread.run(Thread.java:856)
like image 484
Akshay Soam Avatar asked May 29 '14 09:05

Akshay Soam


1 Answers

JSONObject jsonObj = new JSONObject(json);

jsonObj.get("products");

JSONObject newJsonObj = new JSONObject();
newJsonObj.put("whatever", jsonObj.get("products"));

If you want to keep the json without escaping you need to add the jsonObject to an JsonArray.

JSONArray jsonArray = new JSONArray();
jsonArray.put(jsonObj);

and get it back later with

for (int i = 0; i < jsonArray.length(); i++)  { 
    JSONObject jsonObj = jsonArray.getJSONObject(i); 
 // then you can start filtering out the array.
    if (jsonObj.has("products")) {
         JSONArray nJsonArray = new JSONArray(jsonObj.get("products"));
          for (......) {} 
}
like image 195
Emanuel S Avatar answered Oct 02 '22 19:10

Emanuel S