如何修改JSON数据并返回更新的JSON数据 [英] How to modify the JSON data and return the updated JSON data

查看:450
本文介绍了如何修改JSON数据并返回更新的JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要在中间更新JSON数据,并且需要使用java返回更新后的JSON数据.另外,它应该支持任何类型的JSON数据.

We have a requirement to update the JSON data in middle and need to return the updated JSON data using java. Also it should support any type of JSON data.

例如: 假设{object:{"color":"red","shape":"Triangle"}}是JSON数据,在这种情况下,我们需要将shape值更新为Rectangle,并且需要返回更新后的JSON数据,如下所示:

ex: Assume {object:{"color":"red","shape":"Triangle"}} is the JSON data and in this we need to update the shape value to Rectangle and we need to return the updated JSON data as below:

{object:{"color":"red","shape":"Rectangle"}}

为此,我们需要将元素路径(我们需要更新的元素)以及updateText和JSON数据传递给JAVA代码.

For this we need to pass the element path ( which element we need to update) and updateText and JSON Data to the JAVA code.

这是methodCall:

here is the methodCall:

updateValue("object/shape", "Rectangle", "{object:{"color":"red","shape":"Triangle"}}")

我们使用Gson库尝试了以下代码.但是使用此代码,我们能够更新目标Json元素,但要求是使用更新后的值返回整个JSON数据.

We tried below code using Gson library. But with this code we are able to update the targeted Json element, but the requirement is to return the entire JSON data with the updated value.

因此,请提出如何使用更新后的文本重建JSON数据的建议.

So please suggest how do we re-build the JSON data with the updated text.

下面是我们尝试更新Json数据的代码.

Below is the code we tried to update the Json Data.

    public String updateValue(String keyPath, String updateText, String jsonText) {
    String[] keys = keyPath.split("/");
    JsonParser jsonParser = new JsonParser();
    JsonObject jsonObject = (JsonObject) jsonParser.parse(jsonText);
    String result = "";
    for(String key : keys)
    {
        if (jsonObject.get(key) instanceof JsonObject)
        {
          jsonObject = (JsonObject)jsonObject.get(key);
        }
        else if(jsonObject.get(key) instanceof JsonArray)
        {
          JsonArray jsonArray = (JsonArray)jsonObject.get(key);
          result = jsonArray.toString();
        }
        else 
        {
          result = jsonObject.get(key).toString();
        }           
    }
    result = result.replace(result, updateText);
    return result;      
}

推荐答案

问题出在替换方法上.当您将JsonObject转换为String时,您将丢失该对象,并且在替换后,只需替换掉String.要修复它,您需要直接在对象上进行操作,而不是在String对象上进行操作.由于JsonObject是可变的,因此持有对输入的引用将反映所做的更改.一个缺点是您不能以这种方式替换JsonArray中的值,部分原因是您不知道要替换哪个元素.为此,您将需要在输入中多一点(要替换的值或元素位置).

The problem lies in the way you do the replacements. When you translate the JsonObject to String, you lose the object, and after replacement, you just have the replaced String. To fix it, you need to operate directly on the object, instead of the String counterpart. Because JsonObject is mutable, holding a reference to the input will reflect the changes. One drawback is you can't replace a value in a JsonArray this way, partly because you don't know which element to replace. To accomplish that, you will need a little more in the input(either the value to replace or the element position).

public String updateValue(String keyPath, String updateText, String jsonText) {
    String[] keys = keyPath.split("/");
    JsonParser jsonParser = new JsonParser();
    JsonObject jsonObject = (JsonObject) jsonParser.parse(jsonText);
    JsonObject returnVal = jsonObject; // This holds the ref to target json object
    JsonPrimitive jp = new JsonPrimitive(updateText);
    String finalKey = keys[keys.length - 1];
    for(String key : keys)
    {
        if (jsonObject.get(key).isJsonObject())
        {
            jsonObject = (JsonObject)jsonObject.get(key);
        }
    }
    jsonObject.remove(finalKey);
    jsonObject.add(finalKey, jp);
    return returnVal.toString();      
}

这篇关于如何修改JSON数据并返回更新的JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆