我正在尝试从java JSON
创建一个servlet
响应。通过使用我做的一些教程
JSONObject json = new JSONObject();
JSONArray addresses = new JSONArray();
JSONObject address;
try
{
int count = 5;
for (int i=0 ; i<count ; i++)
{
address = new JSONObject();
address.put("CustomerName" , "Decepticons" + i);
address.put("Street" , "Devestator Avenue" + i);
address.put("City" , "Megatron City" + i);
address.put("Country" , "CyberTron" + i);
addresses.add(address);
}
json.put("Addresses", addresses);
}
catch (JSONException jse)
{
}
response.setContentType("application/json");
response.getWriter().write(json.toString());
和输出是
{"Addresses":[{"CustomerName":"Decepticons0","Street":"Devestator Avenue0","City":"Megatron City0","Country":"CyberTron0"},{"CustomerName":"Decepticons1","Street":"Devestator Avenue1","City":"Megatron City1","Country":"CyberTron1"},{"CustomerName":"Decepticons2","Street":"Devestator Avenue2","City":"Megatron City2","Country":"CyberTron2"},{"CustomerName":"Decepticons3","Street":"Devestator Avenue3","City":"Megatron City3","Country":"CyberTron3"},{"CustomerName":"Decepticons4","Street":"Devestator Avenue4","City":"Megatron City4","Country":"CyberTron4"}]}
问题是当我尝试将其解析为我的Android应用程序时,它显示错误为
解析数据org.json.JSONException: Value <html><head><title>Apache of type java.lang.String cannot be converted to JSONObject
我正在使用android代码json解析示例 http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
任何人都可以解决我的问题吗? 提前谢谢。
答案 0 :(得分:0)
尝试
json.toJSONString()
而不是
json.toString()