java 编辑 JSON 字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15334539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:16:45  来源:igfitidea点击:

Edit JSON Fields

javaandroidjson

提问by user1340801

I have a JSON coming from server as below

我有一个来自服务器的 JSON 如下

   {
    "XXXX": {
        "type": "RSS",
        "value": ""
    },
    "YYYY": {
        "type": "String",
        "value": ""
    },
    "ZZZZ": {
        "type": "String",
        "value": ""
    }
}

Now I need to add the Stringvalue in fields of all XXXX, YYYY, and ZZZZ.

现在我需要String在所有 XXXX、YYYY 和 ZZZZ 的字段中添加值。

I'm using eclipse and I need to change the value of "value" in XXXX and YYYY and ZZZZ and I need to add the field

我正在使用 eclipse,我需要更改 XXXX 和 YYYY 和 ZZZZ 中“value”的值,我需要添加字段

{
    "MMMM": {
        "type": "Image",
        "value": "a7e8bec0-87ed-11e2-aa2e-52540025ab96_2_1362746556"
    }
}

After ZZZZ. Please let me know how to do it.

ZZZZ之后。请让我知道该怎么做。

回答by Pragnani

Try this

试试这个

String jsonstring="{
    "XXXX": {
        "type": "RSS",
        "value": ""
    },
    "YYYY": {
        "type": "String",
        "value": ""
    },
    "ZZZZ": {
        "type": "String",
        "value": ""
    }
}";

JSONObject object=new JSONObject(jsonstring);
JSONObject childobject=object.getJSONObject("XXXX");

JSONObject modifiedjson=new JSONObject();
modifiedjson.put("type",childobject.get("type"));
modifiedjson.put("value","newvalue");  // Add new value of XXXX here

//

JSONObject mmjson=new JSONObject();
mmjson.put("type","image");
mmjson.put("value","a7e8bec0-87ed-11e2-aa2e-52540025ab96_2_1362746556");  // Add new value of MMM here

JSONObject newjson=new JSONObject();
newjson.put("MMMM",mmjson.toString());
newjson.put("XXXX",modifiedjson.toString());
newjson.put("YYYY",object.get("YYYY"));
newjson.put("ZZZZ",object.get("ZZZZ"));

回答by karmanaut

I think you meant

我想你的意思是

{"XXXX":
{"type":"RSS","value":"},
"YYYY (mins)":{"type":"String","value":""},
"ZZZZ":{"type":"String","value":""}

is the JSON you get from server. You can always get the JSONObject.toString and edit it as required and then do something like,

是您从服务器获得的 JSON。您始终可以获取 JSONObject.toString 并根据需要对其进行编辑,然后执行类似的操作,

JSONObject obj = new JSONObject(myString);

If you need to add a key value to JSON you may try,

如果您需要向 JSON 添加一个键值,您可以尝试,

JSONObject value = new JSONObject();
value.put("key","value");
value.put("key","value");//add all the field you want for ZZZZ.
obj.put("ZZZZ",value);

回答by Loki

If it is a string, you can just search for the specified values and concat the new string.

如果它是一个字符串,你可以只搜索指定的值并连接新的字符串。

If it is a JSON Object, you could add new Values to the JSON Object and search for the values you want to manipulate and set them new.

如果它是一个 JSON 对象,您可以向 JSON 对象添加新值并搜索要操作的值并将它们设置为新的。

What are you doing at the moment? Can you show us some code, so we know, where exactly you have the problem? And show, how you are accessing the JSON in this code, please.

你此刻在做什么?您能否向我们展示一些代码,以便我们知道您的问题究竟出在哪里?并请说明您如何访问此代码中的 JSON。

回答by Md Abdul Gafur

User Java String replacement method to replace string.

用户 Java 字符串替换方法来替换字符串。

Take you Json as a string then replace value via string replacement method.

将您的 Json 作为字符串,然后通过字符串替换方法替换值。

Here is small example.

这是一个小例子。

String replaceSample = "This String replace Example shows how to replace one char from 

String newString = replaceSample.replace('r', 't');

Thanks.

谢谢。

回答by Umesh Patil

 var source={
                "XXXX": {
                    "type": "RSS",
                    "value": ""
                },
                "YYYY": {
                    "type": "String",
                    "value": ""
                },
                "ZZZZ": {
                    "type": "String",
                    "value": ""
                }
            }

And element you wanted to add

你想添加的元素

 var element={
        "type": "Image",
        "value": "a7e8bec0-87ed-11e2-aa2e-52540025ab96_2_1362746556"
    };

You can do with below script,

您可以使用以下脚本,

source["MMMM"]=element;

or

或者

source.MMMM=element;

回答by RAM

You can do all operations in JavaScript itself.

您可以在 JavaScript 本身中完成所有操作。

Lets store the date coming from server into variable a:

让我们将来自服务器的日期存储到变量中a

var a = {
    "XXXX":{"type":"RSS","value":"},
    "YYYY (mins)":{"type":"String","value":""},
    "ZZZZ":{"type":"String","value":""}
}

To change the value:

要更改值:

a['XXXX']['value'] = 'new_value1';
a['YYYY']['value'] = 'new_value2';
a['ZZZZ']['value'] = 'new_value3';

To add a field:

要添加字段:

a["MMMM"] = {"type":"Image","value":"a7e8bec0-87ed-11e2-aa2e-52540025ab96_2_1362746556"}}