1

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.

Here's the JSON:

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

I'm fairly sure the JSON is correctly formatted.

Here is the Java (for Android SDK, this is in the OnCreate method in main Activity class):

    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.

SpicyKarl
  • 13
  • 1
  • 4

4 Answers4

4

You will probably want to simplify the JSON structure, but you can read it now as follows:

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:

{"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();
}
Aleadam
  • 40,203
  • 9
  • 86
  • 108
  • I see what you mean by simplifying the JSON, but in the future there will be more files listed in the JSON. I am planning on using a "for" loop to parse them. Thanks again friend, I really appreciate your help. – SpicyKarl Apr 28 '11 at 06:21
  • @SpicyKarl of course, that's why is an array. I'm just saying you don't need the numbering labels, e.g. `{"array":[{"item1"}{"item2"}{"item3"}]}` – Aleadam Apr 28 '11 at 06:25
  • Ok, now I see what you mean. I will simplify the JSON as you suggest, this will help me parse multiple entries in the JSONArray. Thank you for everything. – SpicyKarl Apr 28 '11 at 06:34
2

For Getting Single Value You can use JSONTokener:

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

augustine
  • 1,022
  • 1
  • 14
  • 20
1

may this help to u How can I deserialize an array inside a JSON object?

Community
  • 1
  • 1
  • Thank you but I would like to do this without gson. I have added gson to my resources and have been looking at examples, but haven't found a simpler way to do this without gson. – SpicyKarl Apr 28 '11 at 06:00
  • ya to parse json is quite complex, but with the help of Gson it is very easy dear –  Apr 28 '11 at 06:03
0

e.g. to retrieve filename from above json string

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();
        }

sat
  • 40,138
  • 28
  • 93
  • 102