81,092
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] args) throws JSONException {
String data = "[{'goodsId':'11','goodsq':'10'},{'goodsId':'11','goodsq':'10'},{'goodsId':'113','goodsq':'10'},{'goodsId':'112','goodsq':'10'}]";
JSONArray a = new JSONArray(data);
List<String> list = new ArrayList<String>();
for (int i = 0; i < a.length(); i++) {
JSONObject jo = a.getJSONObject(i);
list.add(jo.toString());
}
int count = 0;
for (int i = 0; i < a.length(); i++) {
JSONObject jo = a.getJSONObject(i);
count += list.indexOf(jo.toString());
}
if (count == a.length()) {
System.out.println("没有重复");
} else {
System.out.println("有重复了");
}
}
public static void main(String[] args) {
String data = "[{'goodsId':'11','goodsq':'10'},{'goodsId':'113','goodsq':'10'},{'goodsId':'112','goodsq':'10'},{'goodsId':'11','goodsq':'10'}]";
data = data.replace(" ","").substring(1,data.length()-1); // 去掉头尾[]
int index = 0;
do {
int _index = data.indexOf("},{", index);
// 最后一组不用比较肯定不会有相同的所以直接退出
if (_index <= -1) {
break;
}
String sub_str = data.substring(index,_index+1);
if (data.indexOf(sub_str, _index) > _index) {
System.out.println("有重复数据." + sub_str);
break;
}
index = _index+2;
} while (true);
}
public static void main(String[] args) throws JSONException {
String data = "[{'goodsId':'11','goodsq':'1023','gg':'1231'},{'goodsId':'11','goodsq':'1023','gg':'1231'},{'goodsId':'11','goodsq':'102','gg':'123'}]";
JSONArray a = new JSONArray(data);
List<String> list = new ArrayList<String>();
for (int i = 0; i < a.length(); i++) {
JSONObject jo = a.getJSONObject(i);
String d = returnStr(jo.toString()); // 需要判断 goodsq 以后的 数据重复
if(list.contains(d)){
System.out.println("重复了"+d);
}else{
list.add(d);
System.out.println("没有重复"+d);
}
}
}
private static String returnStr (String srt){
int i= srt.indexOf("goodsq");
srt=srt.substring(i,srt.length());
return srt;
}