Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将字符串与JSON.getString进行比较_Java_Android_Json_Compare - Fatal编程技术网

Java 将字符串与JSON.getString进行比较

Java 将字符串与JSON.getString进行比较,java,android,json,compare,Java,Android,Json,Compare,我在这件事上有点纠结。我试图将字符串与jsonobject.getString进行比较,但无论我做什么,它们似乎都不匹配: String date = scoreJson.getString("Date"); if (dateString.equals(date)) { //do stuff } dateString的值在此处赋值: calendar = Calendar.getInstance(); int day = calendar.get(calendar.DAY_OF_MON

我在这件事上有点纠结。我试图将字符串与jsonobject.getString进行比较,但无论我做什么,它们似乎都不匹配:

String date = scoreJson.getString("Date");
if (dateString.equals(date))
{
    //do stuff
}
dateString的值在此处赋值:

calendar = Calendar.getInstance();
int day = calendar.get(calendar.DAY_OF_MONTH), month = calendar.get(calendar.MONTH), year = calendar.get(calendar.YEAR);

//months start at 0 in calendar
month += 1;

//use builder to create the dateString
builder.append(year).append("-").append(month).append("-").append(day);
dateString = builder.toString();
当我在debug中查看这两个字符串时,它们显示为“2015-05-18”,但它们在条件语句中不匹配。有人知道为什么吗?我如何比较它们

谢谢

编辑:JSON对象将以以下格式返回:

{"ScoreData":{"Username":"testUser","Id":"8fb25209-863a-410b-a440-
b5b57a903ee1","Date":"2015-02-25","Score":"25.3"}}
我在此处从中检索值并保存到sharedprefs:

//response comes back as two JSONObjects, this makes the inner object into a new JSON
JSONObject responseJson = new JSONObject(response);

JSONObject scoreJson = new JSONObject(responseJson.getString("ScoreData"));

String date = scoreJson.getString("Date");
puzzleScore = scoreJson.getString("Score");

SharedPreferencesWrapper.saveToPrefs(c, "score-" + date, puzzleScore);
然后我在这里检查钥匙:

puzzleScore = SharedPreferencesWrapper.getFromPrefs(c, "score-" + dateString, "NotFound");

if (!puzzleScore.contains("NotFound"))
{
    //do stuff
}

您是否尝试过在此
scoreJson.getString(“日期”)之后添加
toString()
,因此代码类似于
scoreJson.getString(“日期”).toString()

我认为这是编码的问题。如果这不起作用,请检查字符串周围是否有空格。您可以使用
trim()
删除起始空格和尾随空格


希望这有帮助,祝你好运~

也许字符串中有不可打印的字符。还有,为什么不改为比较日期实例呢?Json字符串包括“所以在比较之前请尝试替换所有(\”,“”)。@AlexisC。嗯,这是可能的,我使用字符串比较是因为在我的代码中,我需要比较它们的地方是从SharedReferences检索一个项目,例如:
code SharedReferencesWrapper.saveToPrefs(c,“score-”+date,puzzleScore)
code-puzzscore=SharedReferencesWrapper.getFromPrefs(c,“score-”+日期字符串,“NotFound”)通过toString()表示法比较等式很难看。toString()通常用于表示,而不是comaprison。更好的方法是将从JSON获得的日期解析为日历,并比较日历对象。@FilipMalczak。不幸的是,我无法解析到日期,因为我需要匹配两个字符串才能从SharedReferences中提取密钥。