我是 JSON 新手,我想知道如何提取一些信息并将其放入 JSON 对象中,然后读取一些值。json信息看起来像这样,
{
"photos":{
"page":1,
"pages":1802,
"perpage":100,
"total":"180134",
"photo": [
{
"id":"8711964781",
"owner":"21156896@N07",
"secret":"3e24e45977",
"server":"8553",
"farm":9,
"title":"Old Harbor",
"ispublic":1,
"isfriend":0,
"isfamily":0
},
{
"id":"8712680696",
"owner":"21156896@N07",
"secret":"fe82f8387b",
"server":"8122",
"farm":9,
"title":"Rockefeller Plaza",
"ispublic":1,
"isfriend":0,
"isfamily":0
},
....
它的照片阵列持续了很长一段时间。我将如何获得“isPublic”的值?“秘密”是一个字符串,因为“”和isPublic是一个整数还是布尔值?我基本上必须将它们放入 URL 链接并下载图像。
public class ReadString extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... FlickrString) {
try {
json = getValue();
return json.getString(FlickrString[0]);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
tvTest.setText(result);
}
这适用于所有字符串。我如何将农场、isfriend、isfamily 和 ispublic 转换为字符串?我现在尝试了其他方法来获取 Int 值,我创建了一个全新的类来读取 int。但它没有得到正确的 int 值。我让它下载 int 并将其转换为字符串,然后使用 String.valueOf(x); 将 textview 更改为 int 的字符串;我究竟做错了什么?
public class ReadInt extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... FlickrString) {
try {
json = getValue();
int x = json.getInt(FlickrString[0]);
String y = String.valueOf(x);
return y;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
tvTest.setText(result);
}
}
谢谢!