【发布时间】:2013-12-20 21:51:00
【问题描述】:
我在我的 android 代码中创建了一个 JSON 数组并将其发布到我的 servlet,如下所示:
JSONArray Data = new JSONArray();
for (i = 0; i <= 10; i++) {
Data.put(String.valueOf(i));
}
}
URI url = new URI(serveraddress);
HttpPost httpPost = new HttpPost(url);
StringEntity DataEntity = new StringEntity(Data.toString());
DataEntity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
httpPost.setEntity(DataEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
然后在我的 servlet 中,我使用阅读器从 http 请求中获取内容,如下所示:
StringBuffer jb = new StringBuffer();
String line = null;
try {
BufferedReader reader = req.getReader();
while ((line = reader.readLine()) != null)
jb.append(line);
} catch (Exception e) { /*report an error*/ }
现在我不确定如何从这个字符串创建 JSON 数组。 servlet 托管在带有 json_simple 库的 jetty Web 服务器中。 JSONArray 类只有一个空的构造函数,这与 Android 包中的构造函数不同,后者也有一个带有字符串参数的构造函数。那么有人可以给我一个关于如何从字符串创建 JSON 数组的提示吗?
【问题讨论】: