You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将HashMap转为JSONObject时斜杠被转义为\/的原因及解决方案

Hey there! Let's break down why your forward slashes are getting escaped and how to handle this issue.

Why are "/" escaped to "/" when converting HashMap to JSONObject?

The Root Cause

First off, this is actually compliant with JSON standards (RFC 8259). While forward slashes (/) don't need to be escaped in JSON, many JSON libraries—including the org.json JSONObject you're using—automatically escape them to \/. This is mostly a defensive measure to avoid edge-case issues, like when embedding JSON inside HTML (where sequences like </ could be misinterpreted as the end of a tag).

That double backslash you see in your log (\\/) is just Java string escaping at work: the JSONObject produces a string with \/, but when Java prints it, it escapes the backslash itself, so you see \\/. The actual JSON payload only has \/, and any standard JSON parser will convert that right back to / when reading the data—so your original value isn't being altered, just the string representation in logs.

How to Fix It

You have two options depending on your needs:

1. Don't fix it (if parsing works as expected)

If your goal is just to send this JSON to a server or parse it later, you don't need to do anything. When you call jsonObject.getString("SUMHASH") or when a receiving system parses the JSON, the value will be your original string with / intact. The escaping is purely cosmetic in the JSON string representation.

2. Get unescaped slashes in the JSON output

If you specifically need the JSON string to have unescaped /, here are two practical ways to do it:

Option A: Post-process the JSON string

After generating the JSON string, manually replace the escaped slashes with raw ones:

public byte[] getBody() {
    String sumValue= "mZftaLXj7UN19zxc/7n/UZdf.....";
    HashMap<String, String> params2 = new HashMap<>();
    params2.put("***", "*****");
    params2.put("***", "*****");
    params2.put("SUMHASH", sumValue);
    
    JSONObject jsonObject = new JSONObject(params2);
    // Replace escaped slashes with original forward slashes
    String rawJson = jsonObject.toString().replace("\\/", "/");
    Log.d(TAG, "getBody" + rawJson);
    
    try {
        return rawJson.getBytes("utf-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return null;
    }
}

Just note: Use this only if you're sure your strings don't contain intentional \/ sequences that you want to keep.

Option B: Switch to a different JSON library

The org.json library's default behavior is to escape slashes. If you want to avoid manual processing, use a library like Gson or Jackson, which don't escape / by default:

Example with Gson:

import com.google.gson.Gson;

public byte[] getBody() {
    String sumValue= "mZftaLXj7UN19zxc/7n/UZdf.....";
    HashMap<String, String> params2 = new HashMap<>();
    params2.put("***", "*****");
    params2.put("***", "*****");
    params2.put("SUMHASH", sumValue);
    
    Gson gson = new Gson();
    String rawJson = gson.toJson(params2);
    Log.d(TAG, "getBody" + rawJson);
    
    try {
        return rawJson.getBytes("utf-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return null;
    }
}

This will output the JSON with your original / characters untouched.


内容的提问来源于stack exchange,提问作者Aabauser

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:15:49