我可以像这样拿json对象:
{"title":"this i the title", "description":"this is the description"}
甚至json array : data[{"title":"ABCD","name":"Peter"}]
但是我该怎么做:
{"meta":{"total_rows":1,"uri":"\/profile\/info\/","limit":150,"limit_type":"user",
"requests":2,"reset":3063,"recorded":"2010-12-27 22:48:49"}}
例如,我想采取限制,我该怎么办?
这是我可以在互联网上获取json数据的类
public class Connection2 {
float rating;
String name,air_day,network,air_time,description;
public Connection2(String url){
connect_show_info(url);
}
public String returnName(){
return name;
}
private void connect_show_info(String url){
// Create the httpclient
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet(url);
// Execute the request
HttpResponse response;
// return string
String returnString = null;
try {
// Open the webpage.
response = httpclient.execute(httpget);
if(response.getStatusLine().getStatusCode() == 200){
// Connection was established. Get the content.
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
// to worry about connection release
if (entity != null) {
// A Simple JSON Response Read
InputStream instream = entity.getContent();
// Load the requested page converted to a string into a JSONObject.
JSONObject json = new JSONObject(convertStreamToString(instream));
// Get the query value'
String query = json.getString("meta");
// Make array of the suggestions
JSONObject series = json.getJSONObject("series");
// Build the return string.
// Strings
air_day = "monday";
name = series.getString("name");
//air_day = series.getJSONObject(0).getString("air_day").toString() ;
//air_time = series.getJSONObject(0).getString("air_time").toString() ;
//network = series.getJSONObject(0).getString("network").toString();
//description = series.getJSONObject(0).getString("description").toString();
// Int
// Float
//rating = (float) series.getJSONObject(0).optDouble("rating");
// Cose the stream.
instream.close();
}
}
else {
// code here for a response othet than 200. A response 200 means the webpage was ok
// Other codes include 404 - not found, 301 - redirect etc...
// Display the response line.
returnString = "Unable to load page - " + response.getStatusLine();
}
}
catch (IOException ex) {
// thrown by line 80 - getContent();
// Connection was not established
returnString = "Connection failed; " + ex.getMessage();
}
catch (JSONException ex){
// JSON errors
returnString = "JSON failed; " + ex.getMessage();
}
}
这就是我想要的方式:
Connection2 serie = new Connection2(url);
name = (TextView)findViewById(R.id.name);
description = (TextView)findViewById(R.id.description);
airday = (TextView)findViewById(R.id.air_day);
airtime = (TextView)findViewById(R.id.air_time);
name.setText(serie.returnName());
description.setText(serie.description);
答案 0 :(得分:3)
Tsunaze, 取决于您使用的库。以上似乎是 jObject = getJsonObject(“meta”).. jObject.getInteger( “限制”);
首先获取对应于“meta”的json对象,然后从该json对象获取“limit”的值。
答案 1 :(得分:1)
我建议不要编写org.json的低级JSON解析器所需的大量猴子代码,而是建议使用一个支持数据绑定的JSON库,如:
并定义简单的POJO结构,如:
public class Response {
public Meta meta;
}
public class Meta {
public int total_rows;
public String uri; // or could be URL or URI
public int limit;
public String limit_type; // or an Enum of { user, ... }
public int requests;
public int reset;
public Data recorded;
}
然后使用数据绑定,如:
// Jackson; Gson uses 'Gson' object
Response response = new ObjectMapper().readValue(json, Response.class);
如果你想构建请求(或其他JSON),你也可以这样做
byte[] jsonToSend = mapper.writeValueAsBytes(requestObject);
答案 2 :(得分:0)
JSONObject json = new JSONObject(convertStreamToString(instream));
int limit = json.getJSONObject("meta").getInt("limit");