1

我正在尝试JSON从 java 创建响应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

我正在使用来自http://www.androidhive.info/2012/01/android-json-parsing-tutorial/的 android 代码 json 解析示例

任何人都可以解决我的问题吗?提前致谢。

4

1 回答 1

0

尝试

json.toJSONString() 

代替

json.toString()
于 2012-08-11T10:38:00.080 回答