有没有办法,我可以清空整个 JSONObject -- java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31239853/
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-08-11 10:49:49  来源:igfitidea点击:

Is there a way, I can empty the whole JSONObject -- java

javajson

提问by inquisitive

I have to populate a json object like this, let say it is named detailJSON:

我必须像这样填充一个 json 对象,假设它被命名为 detailJSON:

{"amount": "5.00", "ac_no": "123456" } 

I do it this way:

我这样做:

detailJSON.put("amount","5.00");
detailJSON.put("ac_no","123456");

After this, the detail is entered in some shared preferences, and now I want to clearthis JSONObject and use the same detailJSON object to store another json (with different keys), this way:

在此之后,在一些共享首选项中输入了详细信息,现在我想清除此 JSONObject 并使用相同的 detailJSON 对象来存储另一个 json(具有不同的键),如下所示:

{"amount":"6.00", "loan_no":"123456"}

I know there is a method, remove(), that removes the particular key and corresponding value.

我知道有一种方法 remove() 可以删除特定的键和相应的值。

This works:

这有效

detailJSON.remove("amount");
detailJSON.remove("ac_no");

and then use it --

然后使用它——

detailJSON.put("amount","6.0");
detailJSON.put("loan_no","123456");

Now this is a very simple example. In the code I'm working on, I have a lot of keys, so using remove actually increases the LOC. Also, each time before removing I need to check whether JSONObject hasthat particular key or not.

现在这是一个非常简单的例子。在我正在处理的代码中,我有很多键,因此使用 remove 实际上会增加 LOC。此外,每次删除之前,我都需要检查 JSONObject 是否has为该特定键。

Is there any other way, I can implement clearing of the JSONObject??

有没有其他方法,我可以实现 JSONObject 的清除?

I tried

我试过

detailJSON=null ;
detailJSON=new JSONObject();

But it does not work.

但它不起作用。

I am basically in search of something like clear() method, if exists.

我基本上是在寻找类似 clear() 方法的东西,如果存在的话。

采纳答案by Boris Pavlovi?

Iterator keys = detailJSON.keys();
while(keys.hasNext())
  detailJSON.remove((String)detailJSON.keys().next());

回答by Pavan Kumar K

if detailJSONis an map variable, the you can use detailJSON.clear()method to empty values in the map.

如果detailJSON是地图变量,则可以使用detailJSON.clear()方法清空地图中的值。

回答by dkiselev

You could, but it will be a hack.

你可以,但这将是一个黑客。

Iterator i = detailJSON.keys();
while(i.hasNext()) {
    i.next().remove();
}

//or

detailJSON.keySet().clear();

It works, because JSONObject.keySet()will return you this.map.keySet(). And what JavaDoc for HashMap.keySet()said:

它有效,因为JSONObject.keySet()会回报你this.map.keySet()。什么 JavaDocHashMap.keySet()说:

Returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa.

返回此映射中包含的键的 Set 视图。该集合由地图支持,因此对地图的更改会反映在该集合中,反之亦然。

Java's HashMapfrom collections will return you java.util.HashMap.KeySetand java.util.HashMap.KeySet.clear();just calls map.clear()(KeySet is an inner class of java.util.HashMap)

Java 的HashMapfrom 集合将返回您java.util.HashMap.KeySet并且java.util.HashMap.KeySet.clear();只是调用map.clear()(KeySet 是 java.util.HashMap 的内部类)

回答by Muhammad Suleman

you can use this method

你可以用这个方法

public HashMap clearMap(HashMap detailJSON){
  for(String key: detailJSON.keySet())
    detailJSON.remove(key);
  return detailJSON;
}

回答by Bruno Jennrich

this version has an overhead because of getting the key-set everytime, but it works without concurrent modification exception.

由于每次都获取密钥集,此版本有开销,但它可以在没有并发修改异常的情况下工作。

while(json.length()>0)
   json.remove(json.keys().next());

回答by Ratata Tata

It appears there is no clear method for JSONObject:. if you have to iterate all the JSONObjectto clear it, better you get the parent node, and update with a new JSONObject().

似乎没有明确的方法JSONObject:。如果您必须迭代所有JSONObject以清除它,最好获取父节点,并使用new JSONObject().

You probably using whileto get those details, so you can do this:

您可能使用while来获取这些详细信息,因此您可以执行以下操作:

Iterator keys = mJson.keys();
while(keys.hasNext())
{
    String key = keys.next();
    JSONObject detailJSON = mJson.getJSONObject(key);
    JSONObject newDetail = new JSONObject();
    ...
    mJson.put(key,newDetail);
}

And when using JSONArray:

当使用JSONArray

for (int i=0; i < mJson.length(); i++)
{
    JSONObject detailJSON = mJson.getJSONObject(i);
    JSONObject newDetail = new JSONObject();
    ...
    mJson.put(i,newDetail);
}