从字符串创建JSONObject – 在2770处的键后面预期为 ':' [字符2771行1]

huangapple go评论80阅读模式
英文:

Creating a JSONObject from string - Expected a ':' after a key at 2770 [character 2771 line 1]

问题

以下是翻译好的内容:

我正在尝试将由StringBuilder构建的字符串转换为JSONObject。我正在使用以下代码创建JSONObject:

StringBuilder ret = new StringBuilder();
// 一些代码来构建字符串... ...
JSONObject returnObject = null;
try {
    returnObject = new JSONObject(ret.toString());
} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

return returnObject;

问题在于,这段代码给我以下错误:

在2770号字符[第1行]之后预期一个':'。

我正在尝试转换的字符串(样本)如下所示:

[
{
"data":{"PHONE":"4567899874","STATE":"State1","FIRSTNAME":"Test1","EMAIL":null,"CITY":"New York","LASTNAME":"TestLast"}
},
{
"data":{"PHONE":"6457870022","STATE":"State2","FIRSTNAME":"FirstTestUpdate2","EMAIL":"last2first2@abc.com","CITY":"City2","LASTNAME":"LastTestUpdate2"}
},

{
"data":{"PHONE":"6457870003","STATE":"State3","FIRSTNAME":"FirstTestDemo3","EMAIL":"last3first3@abc.com","CITY":"City3","LASTNAME":"Last3"}
},

{"data":{"PHONE":"6457870004","STATE":"State4","FIRSTNAME":"First4","EMAIL":"last4first4@abc.com","CITY":"City4","LASTNAME":"Last4"}
},

{
"data":{"PHONE":"6457870005","STATE":"State5","FIRSTNAME":"First5","EMAIL":"last5first5@abc.com","CITY":"City5","LASTNAME":"Last5"}
},

{
"data":{"PHONE":"6457870006","STATE":"State6","FIRSTNAME":"First6","EMAIL":"last6first6@abc.com","CITY":"City6","LASTNAME":"Last6"}
},

{
"data":{"PHONE":"6845987894","STATE":"State7","FIRSTNAME":"TestFirstSA","EMAIL":null,"CITY":"City7","LASTNAME":"TestSA"}
}

]

当我尝试使用此在线工具转换此字符串时,它可以正确解析。我在stackoverflow上看到了一些其他问题类似于这个,并尝试使用他们的方法,但仍然出现错误。有什么建议可以改变以消除错误?

英文:

I am trying to conver a string build by StringBuilder to JSONObject. The code I am using to create a JSONObject is as follows

StringBuilder ret = new StringBuilder();
//SOME CODE TO MAKE THE STRING ... ...
JSONObject returnObject = null;
		try {
			returnObject = new JSONObject(ret.toString());
		} catch (JSONException  e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return returnObject;

The problem is that this code is giving me following error

Expected a ':' after a key at 2770 [character 2771 line 1]

The string (Sample) I am trying to convert is as follows

[
{
"data":{"PHONE":"4567899874","STATE":"State1","FIRSTNAME":"Test1","EMAIL":null,"CITY":"New York","LASTNAME":"TestLast"}
},
{
"data":{"PHONE":"6457870022","STATE":"State2","FIRSTNAME":"FirstTestUpdate2","EMAIL":"last2first2@abc.com","CITY":"City2","LASTNAME":"LastTestUpdate2"}
},

{
"data":{"PHONE":"6457870003","STATE":"State3","FIRSTNAME":"FirstTestDemo3","EMAIL":"last3first3@abc.com","CITY":"City3","LASTNAME":"Last3"}
},

{"data":{"PHONE":"6457870004","STATE":"State4","FIRSTNAME":"First4","EMAIL":"last4first4@abc.com","CITY":"City4","LASTNAME":"Last4"}
},

{
"data":{"PHONE":"6457870005","STATE":"State5","FIRSTNAME":"First5","EMAIL":"last5first5@abc.com","CITY":"City5","LASTNAME":"Last5"}
},

{
"data":{"PHONE":"6457870006","STATE":"State6","FIRSTNAME":"First6","EMAIL":"last6first6@abc.com","CITY":"City6","LASTNAME":"Last6"}
},

{
"data":{"PHONE":"6845987894","STATE":"State7","FIRSTNAME":"TestFirstSA","EMAIL":null,"CITY":"City7","LASTNAME":"TestSA"}
}

]

When I try to convert this string using This Online Tool then it is parsing correctly. I saw some other questions in stackoverflow like this and tried to use their method but still getting error. Any suggestion what change could make the error go away.

答案1

得分: 1

你已经快要成功了!你使用的库不够智能,就像Jackson一样。你必须手动设置,表明你尝试解析的是JsonArray,而不是JsonObject

String jsonArray = "[]";
JSONArray arr = new JSONArray(jsonArray);

String jsonObject = "{}";
JSONObject obj = new JSONObject(jsonObject);
英文:

You're almost there! Lib you use is not so smart like Jackson. You have to manually set that you try to parse an JsonArray, but not JsonObject.

String jsonArray = "[]";
JSONArray arr = new JSONArray(jsonArray);

String jsonObject = "{}";
JSONObject obj = new JSONObject(jsonObject);

huangapple
  • 本文由 发表于 2020年8月31日 23:39:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63673888.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定