【问题标题】:How to remove escaping slashes from a json inside another json?如何从另一个json中的json中删除转义斜杠?
【发布时间】:2016-08-20 15:43:14
【问题描述】:

我有以下 json,正在填充:

new Gson().toJson(batch.getProducts());

但是当我将它添加到我的主 JSONObject 时,它是 json 的值这一事实正在对所有 json(甚至键)进行转义。

{"name":"Ufud","store":2,"products":"[{\"name\":\"Test2\",\"stock\":2,\"value\":19.9},{\"name\":\"Test2\",\"stock\":2,\"value\":19.9},{\"name\":\"Teste2\",\"stock\":2,\"value\":19.9}]"}

有什么方法可以防止吗?

【问题讨论】:

    标签: json gson


    【解决方案1】:

    如果你以后要操作你的 json,你可以考虑不把它转换成一个字符串,而是一个 JsonElement,它可以很容易地添加到已经存在的 JsonObject 中

    JsonObject mainObject = getMainObject(); //method which creates your main object. You can change it to variable reference
    JsonElement productsElement = new Gson().toJsonTree(batch.getProducts()); //converting your object into JsonElement
    mainObject.add("products", productsElement); //insering products element into object.
    String json = new Gson().toJson(mainObject); //converting object with products element into a json string
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-02
      • 2018-06-01
      • 2018-04-14
      • 1970-01-01
      • 1970-01-01
      • 2016-12-05
      • 2013-01-17
      • 2015-07-27
      相关资源
      最近更新 更多