我正在创建一个必须发送到api的数组。部分json有联系信息。必须像这样发送:
"Dealer" : {
"email" : "mjones@fake - domain.com" ,
"firstName" : "Martin" ,
"lastName" : "Jones" ,
"phone" : " 5555554321 " ,
"company" : "JonesCo Golf" ,
"street" : "554 Elm Street" ,
"city" : "Springfield" ,
"stateProvince" : "Illinois" ,
"postalCode" : "62701"
}
我的json(使用Log.d时)显示如下:
"Dealer":
[
{
"email":"email@email.com",
"firstName":"John",
"lastName":"Doe",
"phone":"0987654321",
"company":"test",
"street":"123 Street",
"city":"myCity",
"stateProvince":"Xy",
"postalCode":"12345"
}
]
而不是经销商有9个对象,它作为经销商返回,其中包含1个包含9个对象的对象。当然,当我将它发送到API时,这将无法正确解析。
我从共享首选项中提取内容并加载创建JSONArray,如下所示:
JSONObject dealer = new JSONObject();
try {
dealer.put("email", salesPerson.get("emailAddress"));
dealer.put("firstName", salesPerson.get("firstName"));
dealer.put("lastName", salesPerson.get("lastName"));
dealer.put("phone", salesPerson.get("mobilePhone"));
dealer.put("street", salesPerson.get("mailingAddress1"));
dealer.put("street2", salesPerson.get("mailingAddress2"));
dealer.put("city", salesPerson.get("city"));
dealer.put("stateProvince", salesPerson.get("state"));
dealer.put("postalCode", salesPerson.get("postalCode"));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("dealer", dealer.toString());
JSONArray dealerJSON = new JSONArray();
dealerJSON.put(dealer);
try {
emailDataObject.put("Dealer", dealerJSON);
} catch (JSONException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
我有一个非常广泛的JSONArray要发送到API,并且正在创建JSON而没有错误。在我将JSONObject添加到JSONArray的任何地方都添加了方括号。
如何防止将额外的方括号添加到json?
答案 0 :(得分:4)
要避免支持者,请不要将对象添加到数组中,而是将其直接添加到对象中。这是一个例子。
JSONObject dealer = new JSONObject();
try {
dealer.put("email", salesPerson.get("emailAddress"));
dealer.put("firstName", salesPerson.get("firstName"));
dealer.put("lastName", salesPerson.get("lastName"));
dealer.put("phone", salesPerson.get("mobilePhone"));
dealer.put("street", salesPerson.get("mailingAddress1"));
dealer.put("street2", salesPerson.get("mailingAddress2"));
dealer.put("city", salesPerson.get("city"));
dealer.put("stateProvince", salesPerson.get("state"));
dealer.put("postalCode", salesPerson.get("postalCode"));
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("dealer", dealer.toString());
try {
emailDataObject.put("Dealer", dealer);
} catch (JSONException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
答案 1 :(得分:0)
我不是真正进入Android,但我可以给你一些指令,我肯定会告诉你,你的JSON概念上是错误的
"Dealer":
{
"email":"email@email.com",
"firstName":"John",
"lastName":"Doe",
"phone":"0987654321",
"company":"test",
"street":"123 Street",
"city":"myCity",
"stateProvince":"Xy",
"postalCode":"12345"
}
第一个例子是不是JSON对象的数组,而是具有许多属性的单个对象
"Dealer":
[
{
"email":"email@email.com",
"firstName":"John",
"lastName":"Doe",
"phone":"0987654321",
"company":"test",
"street":"123 Street",
"city":"myCity",
"stateProvince":"Xy",
"postalCode":"12345"
}
]
这个是一个真正的JSON数组,由一个元素组成,这是你以前的对象
您必须重新考虑在JSON对象中添加元素,以及您真正期望的行为是什么
JSONArray dealerJSON = new JSONArray();
答案 2 :(得分:0)
您正在使用以下代码创建数组:
JSONArray dealerJSON = new JSONArray();
所以不要这样。
这样做:
emailDataObject.put("Dealer", dealer);
您不需要dealerJSON步骤,因为经销商是您想要的JSONObject。