我正在尝试从JSON API中获取所有价值,我已经成功获得了所有这些,除了一个!如您所见,这是服务器的JSON输出。 (我不能改变它)
{
"error":"",
"S8tf":{
"infoToken":"wCfhXe",
"deleteToken":"gzHTfGcF",
"size":122484,
"sha1":"8c4e2bbc0794d2bd4f901a36627e555c068a94e6",
"filename":"Screen_Shot_2013-07-02_at_3.52.23_PM.png"
},
"S29N":{
"infoToken":"joRm6p",
"deleteToken":"IL5STLhq",
"size":129332,
"sha1":"b4a03897121d0320b82059c36f7a10a8ef4c113d",
"filename":"Stockholmsyndromet.docx"
}
}
正如您所看到的,每个字符串/“数组”都以“fileId”开头,这是从服务器中随机生成的。我正在使用下面的代码,我可以看到所有的值,如:文件名,大小,sh1等。但我似乎无法弄清楚如何获得“fileId”。 fileId是(在这个json中)S8tf和S29N
我的代码:
public class FilesActivity extends SherlockActivity {
private static String TAG_FILENAME = "filename";
private static String TAG_SIZE = "size";
private static String TAG_ITOKEN = "infoToken";
private static String TAG_DTOKEN = "deleteToken";
private static String TAG_SHA1 = "sha1";
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dblist);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Files");
String response = null;
DefaultHttpClient httpClient = new DefaultHttpClient();
ResponseHandler <String> resonseHandler = new BasicResponseHandler();
HttpPost postMethod = new HttpPost("http://api.bayfiles.net/v1/account/files?session=of1903u3pj43c3can8rc33gc42");
try {
JSONObject json = new JSONObject();
json.put("filename", "error");
postMethod.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));
postMethod.setHeader( "Content-Type", "application/json" );
response = httpClient.execute(postMethod,resonseHandler);
TextView txt = (TextView)findViewById(R.id.nodata);
JSONObject request = new JSONObject(response);
for (Iterator<?> keyIterator = request.keys(); keyIterator.hasNext(); ) {
String key = (String) keyIterator.next();
JSONObject object = request.optJSONObject(key);
ArrayList<fileObject> objectList = new ArrayList<fileObject>();
if (object != null) {
//Setting TAGs
TAG_FILENAME = object.getString("filename");
TAG_SIZE = object.getString("size");
TAG_ITOKEN = object.getString("infoToken");
TAG_DTOKEN = object.getString("deleteToken");
TAG_SHA1 = object.getString("sha1");
txt.setText(
TAG_FILENAME + "\n"
+ TAG_SIZE + "\n"
+ TAG_ITOKEN + "\n"
+ TAG_DTOKEN + "\n"
+ TAG_SHA1 + "\n"
+ txt.getText()
);
Log.d("log_tag", object.getString("filename"));
}
}
}
catch(Exception e)
{
e.printStackTrace();
Log.d("log_tag", "Error: " + e.toString());
}
}
}
如果你没有得到它,我试图抓住fileId又名S8tf和S29N的值,这些是随机数并由服务器生成。
编辑:感谢你们工作吧!因为我知道海报弄清楚它是令人恼火的,你不能,我做的是把String fileId = key;在我的对象循环中,像这样:if (object != null) {
fileObject obj = new fileObject();
obj.setFileId(key);
obj.setFileName(object.getString("filename"));
obj.setSize(object.getString("size"));
obj.setInfoToken(object.getString("infoToken"));
obj.setDeleteToken(object.getString("deleteToken"));
obj.setSha1(object.getString("sha1"));
objectList.add(obj);
Log.d("fileId", key); // Shows both of the values!
}
答案 0 :(得分:2)
我觉得getNames()方法可能就是你要搜索的内容。
答案 1 :(得分:0)
如果你看一下for循环的第一部分......
JSONObject request = new JSONObject(response);
for (Iterator<?> keyIterator = request.keys(); keyIterator.hasNext(); ) {
String key = (String) keyIterator.next();
JSONObject object = request.optJSONObject(key);
您从JSONObject请求代表服务器响应的所有密钥。这些键是您要查找的值。如果你要添加
System.out.println(key);
在循环中的第一行之后,您将看到打印出的值('错误','S8tf',...)。