比较来自 JSON 解析器的值
我编写了一个 java 程序,它有一个 json 解析器来获取一些数据。我的问题是比较 json 解析器的值。这是我的代码:
private ArrayList<HashMap<String, String>> listInfo = new ArrayList<HashMap<String, String>>();
public ArrayList<HashMap<String, String>> info() {
//Get the data
JSONObject json = JSONfunctions.getJSONfromURL(URL);
try {
//Get the elements
HashMap<Integer, String> mapValueForCompare = new HashMap<Integer, String>();
JSONArray data = json.getJSONArray("data");
//Loop the array
for (int i = 0; i < data.length(); i++) {
HashMap<String, String> mapInfo = new HashMap<String, String>();
JSONObject e = data.getJSONObject(i);
mapInfo.put("id", e.getString("id"));
mapInfo.put("title", e.getString("title"));
mapInfo.put("value", e.getString("value"));
int t = check(mapValueForCompare, mapInfo);
if (t == 1) {
mapInfo.put("isSame?", "yes");
} else {
mapInfo.put("isSame?", "no");
}
mapValueForCompare.put(i, e.getString("value"));
listInfo.add(mapInfo);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return listInfo;
}
private int check(HashMap<Integer, String> mapValueForCompare, HashMap<String, String> mapInfo) {
int result = -1;
for (int i = 0; i < mapValueForCompare.size() - 1; i++) {
if (mapValueForCompare.get(i) == mapInfo.get("value")) {
result = 1;
} else {
result = 0;
}
}
return result;
}
您会看到我使用 HashMap(mapValueForCompare) 来收集所有值并将每个值与新 HashMap(mapInfo) 的值进行比较。结果只是回答“不”!为什么会这样?我的错在哪里?你有更好的主意吗?我找不到其他东西了。
I write a java program that has a json parser to get some data. My problem is the comparison of values from the json parser. This is my code:
private ArrayList<HashMap<String, String>> listInfo = new ArrayList<HashMap<String, String>>();
public ArrayList<HashMap<String, String>> info() {
//Get the data
JSONObject json = JSONfunctions.getJSONfromURL(URL);
try {
//Get the elements
HashMap<Integer, String> mapValueForCompare = new HashMap<Integer, String>();
JSONArray data = json.getJSONArray("data");
//Loop the array
for (int i = 0; i < data.length(); i++) {
HashMap<String, String> mapInfo = new HashMap<String, String>();
JSONObject e = data.getJSONObject(i);
mapInfo.put("id", e.getString("id"));
mapInfo.put("title", e.getString("title"));
mapInfo.put("value", e.getString("value"));
int t = check(mapValueForCompare, mapInfo);
if (t == 1) {
mapInfo.put("isSame?", "yes");
} else {
mapInfo.put("isSame?", "no");
}
mapValueForCompare.put(i, e.getString("value"));
listInfo.add(mapInfo);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return listInfo;
}
private int check(HashMap<Integer, String> mapValueForCompare, HashMap<String, String> mapInfo) {
int result = -1;
for (int i = 0; i < mapValueForCompare.size() - 1; i++) {
if (mapValueForCompare.get(i) == mapInfo.get("value")) {
result = 1;
} else {
result = 0;
}
}
return result;
}
You see that i use a HashMap(mapValueForCompare) to collect all values and compare each value with the value of the new HashMap(mapInfo). The result is only the answer "no"! Why this? Where is my wrong? Do you have a better idea? I can't find anything else.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更改检查方法:
if (mapInfoTest.get(i) == mapInfo.get("value"))
为
if (mapInfoTest.get(i).equals(mapInfo.get("value")))
您可以在地图上使用
containsKey
来简化代码Change in your check method:
if (mapInfoTest.get(i) == mapInfo.get("value"))
to
if (mapInfoTest.get(i).equals(mapInfo.get("value")))
You can simplify your code using
containsKey
on your maps