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.

2
  • You want to retrieve filename from the above json object ? Commented Apr 28, 2011 at 5:48
  • I'm sorry, I put in the wrong code. I will update it. Commented Apr 28, 2011 at 5:54

4 Answers 4

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();
}
Sign up to request clarification or add additional context in comments.

3 Comments

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 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"}]}
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.
2

For Getting Single Value You can use JSONTokener:

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

Comments

1

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

2 Comments

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.
ya to parse json is quite complex, but with the help of Gson it is very easy dear
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();
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.