问题描述
String json ={\Sign_In_Type\:\ Action\ \ Sign_In_Type\:\ Action\};
当我尝试创建JSONObject时,会正确地抛出异常:
try {
JSONObject json_obj = new JSONObject(json);
String type = json_obj.getString(Sign_In_Type);
} catch(JSONException e){
throw new RuntimeException(e);
}
错误:
线程中的异常mainjava.lang.RuntimeException:org.json.JSONException:复制密钥Sign_In_Type
在com.campanja.app.Upload.main(Upload.java:52)
导致通过:org.json.JSONException:在org.json.JSONObject.putOnce(JSONObject.java:1076)上的复制密钥Sign_In_Type
在org.json.JSONObject(JSONObject.java:205)
在org.json.JSONObject(JSONObject.java:402)
在com.campanja.app.Upload.main(Upload.java:49)
在将其转换为JSONOBject之前,是否有一种智能的删除或检查重复项的方法?
我试图创建:
Set set = new HashSet(Arrays.asList(json));
但是这给了我:
[ {Sign_In_Type:Action,Sign_In_Type:Action}]
任何建议欢迎,谢谢!
两个选项我可以想到蝙蝠:
- 使用正则表达式或令牌来解析字符串,将每个键值对添加到一个hashmap,最后重新创建JSON文档,删除重复的对象。在这种情况下,我只会删除完全相同的键值对。
- 下载
org.json.JSONObject $ c $的源代码c>,并对代码稍作修改以自动省略重复。这有点危险。另一个选择是创建一个简单地验证和修改的修改版本。
扩展JSONObject工作示例
以下代码允许您使用包含重复键的字符串创建JSONOBbject。仅当具有两个具有相同键值但键值不同的键值时才抛出异常。这是因为我认为随机选择两个应该分配哪一个是一个问题(例如后期值?)。当然,这可以根据你的需要改变工作(例如保持多个键的最后值)。
修改的类
import org.json.JSONException;
import org.json.JSONObject;
public class JSONObjectIgnoreDuplicates extends JSONObject {
public JSONObjectIgnoreDuplicates(String json){
super(json);
}
public JSONObject putOnce(String key,Object value)throws JSONException {
Object storedValue;
如果(key!= null&& value!= null){
if((storedValue = this.opt(key))!= null){
if(!storedValue。 equals(value))//只有通过异常对于具有相同键的不同值
throw new JSONException(Duplicate key \+ key +\);
else
返回此;
}
this.put(key,value);
}
返回此;
}
}
主要方法 p>
String json ={\Sign_In_Type\:\Action\,\Sign_In_Type\ :\行动\};
try {
JSONObject json_obj = new JSONObjectIgnoreDuplicates(json);
String type = json_obj.getString(Sign_In_Type);
} catch(JSONException e){
throw new RuntimeException(e);
}
I have a Json String with duplicate values:
String json = "{\"Sign_In_Type\":\"Action\",\"Sign_In_Type\":\"Action\"}";
that correctly throws an exception when I try to create a JSONObject:
try { JSONObject json_obj = new JSONObject(json); String type = json_obj.getString("Sign_In_Type"); } catch (JSONException e) { throw new RuntimeException(e); }
Error:
Exception in thread "main" java.lang.RuntimeException: org.json.JSONException: Duplicate key "Sign_In_Type" at com.campanja.app.Upload.main(Upload.java:52) Caused by: org.json.JSONException: Duplicate key "Sign_In_Type" at org.json.JSONObject.putOnce(JSONObject.java:1076) at org.json.JSONObject.(JSONObject.java:205) at org.json.JSONObject.(JSONObject.java:402) at com.campanja.app.Upload.main(Upload.java:49)
Is there a smart way of removing or checking for duplicates before I convert it to a JSONOBject?I have tried to create:
Set set = new HashSet(Arrays.asList(json));
but that gives me:
[{"Sign_In_Type":"Action","Sign_In_Type":"Action"}]
Any suggesstions welcome, thanks!
Two options I can think of right off the bat:
- Parse the string using wither regex or tokens, add each key-value pair to a hashmap, and in the end recreate your JSON document with the duplicates removed. In this case though I would only remove key-value pairs that are exactly the same.
- Download the source code for
org.json.JSONObject
, and make a slight modification to the code to automatically leave out duplicates. This is a bit dangerous though. Another option is to create a modified version that simply validates and modifies.
Extending JSONObject Working Example
The below code allows you to create a JSONOBbject with a string containing duplicate keys. Exceptions are thrown only when you have two key-values that have the same key, but different values. This was because I think it would be a problem to choose at random which of the two should be assigned (e.g. the later value?). Of course this can be changed to work as you wish (e.g. keep last value for multiple keys).
Modified Class
import org.json.JSONException;
import org.json.JSONObject;
public class JSONObjectIgnoreDuplicates extends JSONObject {
public JSONObjectIgnoreDuplicates(String json) {
super(json);
}
public JSONObject putOnce(String key, Object value) throws JSONException {
Object storedValue;
if (key != null && value != null) {
if ((storedValue = this.opt(key)) != null ) {
if(!storedValue.equals(value)) //Only through Exception for different values with same key
throw new JSONException("Duplicate key \"" + key + "\"");
else
return this;
}
this.put(key, value);
}
return this;
}
}
Main method
String json = "{\"Sign_In_Type\":\"Action\",\"Sign_In_Type\":\"Action\"}";
try {
JSONObject json_obj = new JSONObjectIgnoreDuplicates(json);
String type = json_obj.getString("Sign_In_Type");
} catch (JSONException e) {
throw new RuntimeException(e);
}
这篇关于从Java中的Json String中删除重复项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!