3
 JSONArray albumarray=new JSONArray();
   JSONObject imgobj=new JSONObject();
    imgobj.put("thumb", filepath.get(i));
    imgobj.put("main", filepath.get(i));
    albumarray.put(imgobj);
    JSONObject albumjson=new JSONObject();
   albumjson.put(albumname,albumarray);

When I convert albumjson to string using

albumjson.toString()

I am getting output as below.

{\"test2\":\"[{\\\"thumb\\\":\\\"http:\\\\\\/\\\\\\/dev.mysite.in\\\\\\/mysite\\\\\\/sites\\\\\\/default\\\\\\/files\\\\\\/512da541b31fe.jpg\\\",\\\"main\\\":\\\"http:\\\\\\/\\\\\\/dev.mysite.in\\\\\\/mysite\\\\\\/sites\\\\\\/default\\\\\\/files\\\\\\/512da541b31fe.jpg\\\"}]\"}

the correct format i need is

{"test2":[{"thumb":"http://dev.mysite.in/mysite/sites/default/files/512d9bdced1f2.jpg","main":"http://dev.mysite.in/mysite/sites/default/files/512d9bdced1f2.jpg"},{"thumb":"http://dev.mysite.in/mysite/sites/default/files/512d9be134cb8.jpg","main":"http://dev.mysite.in/mysite/sites/default/files/512d9be134cb8.jpg"}]}

How to replace additional slashes.

Pramod J George
  • 1,723
  • 12
  • 24

2 Answers2

2

PLease use JSONObject.getString('keyName') method instead of toString()

EDIT:

You should first understand why those extra \\ are showing up.It is an escape character for ".Hence,it is very much required there and is a part of JSON encoding .Hence,one should always use the above method to get values of keys whenever needed.

apart from that you can try :

JSONObject.toString(4) where 4 is actually indent spaces and see whether it helps.Otherwise there's simply no other option than to replace those extra \\ like

myJsonString.replaceAll("\\","");

or

 myJsonString=myJsonString.replaceAll("\\\\",""); 

SECOND EDIT:

The string you are sending is perfect to send to any server.You need to decode that string at the server end to JSON and then utilise it.

If you are using .NET you can see this. Or if you are on some other platform you need to find out how to decode to JSON on that platform.

Nezam
  • 4,122
  • 3
  • 32
  • 49
  • But I need the entire string, not value inside the jsonobject – Pramod J George Feb 27 '13 at 07:21
  • you should not do that.You need to go through the object and fetch what you want.Not stringify whole object.That's not the correct way to go about.However,if you need the whole object in string why convert/parse it into `JSONObject` anyways? – Nezam Feb 27 '13 at 07:24
  • I want the entire string and need it as jsonobject – Pramod J George Feb 27 '13 at 07:30
  • I tried your answer but i am getting like this {\"tet\":[{\"thumb\":\"http:\\/\\/dev.lrcdn.in\\/shiaspark\\/sites\\/default\\/files\\/512db57705eb2.jpg\",\"main\":\"http:\\/\\/dev.lrcdn.in\\/shiaspark\\/sites\\/default\\/files\\/512db57705eb2.jpg\"},{\"thumb\":\"http:\\/\\/dev.lrcdn.in\\/shiaspark\\/sites\\/default\\/files\\/512db58309b40.jpg\",\"main\":\"http:\\/\\/dev.lrcdn.in\\/shiaspark\\/sites\\/default\\/files\\/512db58309b40.jpg\"}]} how to get rid of "\\" – Pramod J George Feb 27 '13 at 07:31
  • what did you provide in `getString`? – Nezam Feb 27 '13 at 07:35
  • the key for getting the jsonobject – Pramod J George Feb 27 '13 at 07:45
  • I see nothing wrong with the OP's intent - they're trying to build a json string in a safe way. – Eric Feb 27 '13 at 07:47
  • @Eric the answer is *not* wrong.I am explaining him the proper way to fetch the values.. and yeah he is **ALREADY** building it in a safeway because THATS VALID JSON STRING.. – Nezam Feb 27 '13 at 07:54
  • it means somewhere he is trying to or would try to parse out that keys from the JSON he is getting. – Nezam Feb 27 '13 at 07:55
  • Hi, Actually I want to send the json to a server, and what i given is the content reaching on the server side. When I put it in online jsonviewer is shows invalid json. – Pramod J George Feb 27 '13 at 08:01
-1

There are two things going on here:

  1. Your tools are confusing you. When it shows the output:

    "{\"test2\":\"[{\\\"thumb\\\":\\\"http:\\\\\\/\\\\\\/dev.lrcdn.in\\\\\\/shiaspark\\\\\\/sites\\\\\\/default\\\\\\/files\\\\\\/512da541b31fe.jpg\\\",\\\"main\\\":\\\"http:\\\\\\/\\\\\\/dev.lrcdn.in\\\\\\/shiaspark\\\\\\/sites\\\\\\/default\\\\\\/files\\\\\\/512da541b31fe.jpg\\\"}]\"}"
    

    It is telling you that the result is a string containing:

    {"test2":"[{\"thumb\":\"http:\\\/\\\/dev.lrcdn.in\\\/shiaspark\\\/sites\\\/default\\\/files\\\/512da541b31fe.jpg\",\"main\":\"http:\\\/\\\/dev.lrcdn.in\\\/shiaspark\\\/sites\\\/default\\\/files\\\/512da541b31fe.jpg\"}]"}
    
  2. Taking that string and formatting it:

    {"test2":
     "[{\"thumb\":\"http:\\\/\\\/dev.lrcdn.in\\\/shiaspark\\\/sites\\\/default\\\/files\\\/512da541b31fe.jpg\",\"main\":\"http:\\\/\\\/dev.lrcdn.in\\\/shiaspark\\\/sites\\\/default\\\/files\\\/512da541b31fe.jpg\"}]"
    }
    

    We can see that you've constructed a json object containing a json-encoded string, rather than a nested jsonobject. For whatever reason, your code is having the effect of:

    JSONArray albumarray=new JSONArray();
    JSONObject imgobj=new JSONObject();
    imgobj.put("thumb", filepath.get(i));
    imgobj.put("main", filepath.get(i));
    albumarray.put(imgobj);
    
    JSONObject albumjson = new JSONObject();
    albumjson.put(albumname, albumarray.toString());
    

    That sounds like a bug in your json library

Eric
  • 95,302
  • 53
  • 242
  • 374