这是我从服务器获取响应的 java 代码。
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
HttpPost httppost = new HttpPost("http://www.def.net/my_script.php");
response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
String s = EntityUtils.toString(entity);
JSONObject json = (JSONObject)new JSONParser().parse(s);
System.out.println("News title= " + json.get("news_title"));
System.out.println("Content= " + json.get("news_content"));
}
catch (Exception e)
{
e.printStackTrace();
}
这是 php 脚本返回的内容。
{ "desktop_app_newsfeed":
[
{ "news_id": "132",
"news_title": "test1",
"news_content": "Lorem ipsum dolor sit amet Donec...",
"news_date": "2013-07-18 10:38:20" },
{ "news_id": "1",
"news_title": "Hello world!",
"news_content":"Lorem ipsum dolor sit amet Donec...",
"news_date": "2013-04-22 17:54:05"
}
]
}
如何迭代以分配给java中的变量。