简体   繁体   English

Java JSON 从 JSONArray 中的 JSONObject 中的选定名称中提取值

[英]Java JSON pulling value from selected name in JSONObject from JSONArray

I'm trying to pull a value from a name in a JSONbject that is created from a JSONArray, the JSONAarray is created from the main(root) JSONObject.我正在尝试从从 JSONArray 创建的 JSONbject 中的名称中提取一个值,JSONAarray 是从 main(root) JSONObject 创建的。

Here's the JSON:这是 JSON:

{"filelist": [{
"1": {
    "filename": "sample.mp3",
    "baseurl": "http://etc.com/"
}}]}

I'm fairly sure the JSON is correctly formatted.我相当确定 JSON 的格式正确。

Here is the Java (for Android SDK, this is in the OnCreate method in main Activity class):这是 Java(对于 Android SDK,这是在主 Activity 类的 OnCreate 方法中):

    String jsonString = new String("{\"filelist\": [{ \"1\": { \"filename\": \"sample.mp3\", \"baseurl\": \"http://etc.com/\" }}]}");
JSONObject jObj = new JSONObject(jsonString);
JSONArray jArr = new JSONArray(jObj.getJSONArray("filelist").toString());
JSONObject jSubObj = new JSONObject(jArr.getJSONObject(0).toString());
textView1.setText(jSubObj.getString("filename"));

Thanks for taking a look and any answers are much appreciated.感谢您查看,非常感谢任何答案。

You will probably want to simplify the JSON structure, but you can read it now as follows:您可能希望简化 JSON 结构,但您现在可以阅读如下:

JSONObject jObj;
try {
  jObj = new JSONObject(jsonString);
  JSONArray jArr = jObj.getJSONArray("filelist");
  JSONObject jObj2 = jArr.getJSONObject(0);
  textView1.setText(jObj2.getJSONObject("1").getString("filename"));
} catch (JSONException e) {
  e.printStackTrace();
}

If you are going to have consecutive numbers in the JSON array, then you might consider eliminating them:如果您要在 JSON 数组中有连续的数字,那么您可以考虑消除它们:

{"filelist": [
  {
    "filename": "sample.mp3",
    "baseurl": "http://etc.com/"
  }
]}

Would require one less step:将需要少一步:

JSONObject jObj;
try {
  jObj = new JSONObject(jsonString);
  JSONArray jArr = jObj.getJSONArray("filelist");
  JSONObject jObj2 = jArr.getJSONObject(0);
  textView1.setText(jObj2.getString("filename"));
} catch (JSONException e) {
  e.printStackTrace();
}

For Getting Single Value You can use JSONTokener:对于获取单个值,您可以使用 JSONTokener:

JSONObject object = (JSONObject) new JSONTokener("JSON String").nextValue();
String lstatus=object.getString("filename");

may this help to u How can I deserialize an array inside a JSON object?这对你有帮助吗?

eg to retrieve filename from above json string例如从 json 字符串上方检索文件名

try {
            String jsonString = new String("{\"filelist\": [{ \"1\": { \"filename\": \"sample.mp3\", \"baseurl\": \"http://www.hostchick.com/deemster/\" }}]}");
            JSONObject jObj = new JSONObject(jsonString);
            JSONArray jArr;
            jArr = jObj.getJSONArray("filelist");
            JSONObject jobj = jArr.getJSONObject(0);
            String filename =  jobj.getJSONObject("1").getString("filename");
            Toast.makeText(this, filename, Toast.LENGTH_SHORT).show(); 
        } catch (JSONException e) {
            e.printStackTrace();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM