0

When converting HashMap to JSONObject, every / in the string will replacing with this \/why is like this?? any solution for this? my string is

 String sumValue= "mZftaLXj7UN19zxc/7n/UZdf....";

but i'm getting like this

D/b: getBody{"****":"*****","*****":"***",SUMHASH":"mZftaLXj7UN19zxc\/7n\/UZdf****"}

I'm tried like this

 public byte[] getBody() {

  String sumValue= "mZftaLXj7UN19zxc/7n/UZdf.....";

                    HashMap<String, String> params2 = new HashMap<String, String>();
                    params2.put("***", "*****");
                    params2.put("***", "*****");
                    params2.put("SUMHASH", sumValue);

                    Log.d(TAG, "getBody" + new JSONObject(params2));

                    try {
                        return new JSONObject(params2).toString().getBytes("utf-8");
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                        return null;
                    }}

Given below is the output while doing

System.out.println("getBody" + new JSONObject(params2)); 

enter image description here

Aabauser
  • 533
  • 1
  • 4
  • 17

2 Answers2

2

It is just text representation of the result JSON. Really field SUMHASH contains no additional '\'.

Put object new JSONObject(params2) to new variable and try to get value of field SUMHASH from this object and you see original value of this field.

Anton Tupy
  • 951
  • 5
  • 16
  • The problem is when I'm doing new JSONObject(params2).toString() i'm getting the value with extra `\/`. what i want is, whatever i'm giving the same should be there. while converting toString – Aabauser Jan 04 '18 at 13:11
  • @Aabauser - It is NOT a problem. The JSON spec says that `"\/"` is a correct way to represent a forward slash in a JSON string. Look at the JSON syntax specs: http://json.org/ – Stephen C Jan 04 '18 at 13:18
  • @Aabauser - this is correct textual representation of your JSON. Field value in JSON is correct and do not contains wrong symbols. – Anton Tupy Jan 04 '18 at 13:21
  • `return new JSONObject(params2).toString().getBytes("utf-8");` this returing a wrong value why?? – Aabauser Jan 05 '18 at 04:09
0

Anton Tupy is right. There is no additional '\' in your output. You can test it by using System.out.println().

System.out.println("getBody" + new JSONObject(params2));

And here is the output

getBody{"SUMHASH":"mZftaLXj7UN19zxc/7n/UZdf.....","***":"*****"}
anL
  • 1,073
  • 1
  • 11
  • 31