0

我的客户端程序是这样的:

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse res = httpClient.execute(req); 

服务器端,我编写了一个 JSON 响应。

JSONObject JObject = new JSONObject();
JObject.put("ResponseCode", "100");
JObject.put("Status", "Success");
response.setContentType("application/json");    
PrintWriter out = response.getWriter();
out.print(JObject);
out.flush();

我如何才能阅读此内容ResponseCodeStatus在客户端返回。

4

3 回答 3

0

您必须将 JSOn 字符串转换为对象。如果使用 Javascript,您可以使用JSON.parse(json_str)或者$.parseJSON如果您使用 jQuery。完成后,您可以照常访问这些条目:

var obj = JSON.parse(json_str);
var code = obj["ResponseCode"];

编辑

使用 Java:

JSONObject obj = new JSONObject(json_string);
String code = obj.getString("ResponseCode");
于 2013-05-10T11:39:08.960 回答
0
HttpResponse res = client.execute(req);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

String line = "";
line = rd.readLine();  /// Json Data
于 2013-05-10T11:41:06.667 回答
0

像这样的东西:

StatusLine statusLine = res.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) // or whatever 
                {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content, "utf-8"));
String line;
while ((line = reader.readLine()) != null) 
{
    builder.append(line);
}

sRet = builder.toString();

...

tknr = new JSONTokener(sRet);
于 2013-05-10T11:41:15.950 回答