如何从JSON字符串中的特定值替换双引号?

时间:2018-01-22 12:18:47

标签: json

请参考下面给出的json,我使用 gson.tojson(jsonString)后,所有键值都是双引号

{
        "ctid": "78b2aa1e7fb04942bf4b7d4771143f2c",
        "id": "d27aa45cc4df4b62bc8e221b17d485c5",
        "uid": "",
        "msisdn": "9886121212",
        "status": "ACTIVE",
        "actualAmount": "10.0",
        "currency": "USD",
        "userinfo": "{\"UID\":\"dfjnbfnofnv61\",\"TRANSID\":\"898097261\",\"COTENT_ID\":{\"contentid\":\"3019154\",\"contentname\":\"00000004-TheGameofLifeHD03019154\",\"contenttype\":\"Game\"},\"CP_NAME\":\"FT61\"}"
    }

我的要求是仅从userinfo值中删除双引号, 以下给出

{
    "ctid": "b561400254a9498791b43e2f1ecb1813",
    "id": "813c04d7dae447ea938794024d0b0f78",
    "uid": "",
    "msisdn": "9886121212",
    "status": "ACTIVE",
    "actualAmount": "30.0",
    "currency": "USD",
    "version":"v2",
    "userinfo": {
        "CP_NAME": "Gameloft",
        "contenttype": "Game",
        "CONTENT_ID": {
            "contentid": "3019154",
            "contentname": "00000004-TheGameofLifeHD03019154"
        }
    }
}

两者都是有效的json,请你帮忙解决一下。

感谢

1 个答案:

答案 0 :(得分:1)

这是对我有用的解决方案。

             //first take the jsonResponse
            JsonElement jsonElement = new JsonParser().parse(jsonResponse);
            JsonElement userInfoJsonElement = new JsonParser().parse(paramMap.get("FILTERED_USERINFO"));
            //second remove the key-value(String) pair from json 
            jsonElement.getAsJsonObject().remove("userinfo");
            //third add the json element back as key-value(jsonElement)
            jsonElement.getAsJsonObject().add("userinfo", userInfoJsonElement);