21

I want to read this JSON lines but because it start with JSONArray i'm a little confused

 "abridged_cast": [
 {
 "name": "Jeff Bridges",
 "id": "162655890",
 "characters": [
 "Jack Prescott"
 ]
 },
 {
 "name": "Charles Grodin",
 "id": "162662571",
 "characters": [
 "Fred Wilson"
 ]
 },
 {
 "name": "Jessica Lange",
 "id": "162653068",
 "characters": [
 "Dwan"
 ]
 },
 {
 "name": "John Randolph",
 "id": "162691889",
 "characters": [
 "Capt. Ross"
 ]
 },
 {
 "name": "Rene Auberjonois",
 "id": "162718328",
 "characters": [
 "Bagley"
 ]
 }
 ],

i just need to use the "name" and save all as one String. (the string value will be : Jeff Bridges,Charles Grodin,Jessica Lange,John Randolph,Rene Auberjonois).

this is my code:

try {
 //JSON is the JSON code above
 JSONObject jsonResponse = new JSONObject(JSON);
 JSONArray movies = jsonResponse.getJSONArray("characters");
 String hey = movies.toString();
 } catch (JSONException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
AskNilesh
69.9k16 gold badges134 silver badges171 bronze badges
asked Jun 16, 2013 at 19:13
5
  • can you post the full json string? Commented Jun 16, 2013 at 19:22
  • You are retrieving the array by the wrong name. Commented Jun 16, 2013 at 19:29
  • Should be getJSONArray("abridged_cast") and then for-loop Commented Jun 16, 2013 at 19:30
  • 1
    I accidentally wrote "characters" in the code i ment to wrote "name". Commented Jun 16, 2013 at 19:45
  • Possible duplicate of android how to convert json array to string array Commented Mar 29, 2017 at 22:56

3 Answers 3

68

If you're after the 'name', why does your code snippet look like an attempt to get the 'characters'?

Anyways, this is no different from any other list- or array-like operation: you just need to iterate over the dataset and grab the information you're interested in. Retrieving all the names should look somewhat like this:

List<String> allNames = new ArrayList<String>();
JSONArray cast = jsonResponse.getJSONArray("abridged_cast");
for (int i=0; i<cast.length(); i++) {
 JSONObject actor = cast.getJSONObject(i);
 String name = actor.getString("name");
 allNames.add(name);
}

(typed straight into the browser, so not tested).

answered Jun 16, 2013 at 19:32
Sign up to request clarification or add additional context in comments.

2 Comments

what about the json of this format [{"id":"1","user":"alll"},{"id":"1","user":"alll"}]
@numerah: you have an array without a key, so just parse the json directly into a JSONArray.
9

getJSONArray(attrname) will get you an array from the object of that given attribute name in your case what is happening is that for

{"abridged_cast":["name": blah...]}
^ its trying to search for a value "characters"

but you need to get into the array and then do a search for "characters"

try this

String json="{'abridged_cast':[{'name':'JeffBridges','id':'162655890','characters':['JackPrescott']},{'name':'CharlesGrodin','id':'162662571','characters':['FredWilson']},{'name':'JessicaLange','id':'162653068','characters':['Dwan']},{'name':'JohnRandolph','id':'162691889','characters':['Capt.Ross']},{'name':'ReneAuberjonois','id':'162718328','characters':['Bagley']}]}";
 JSONObject jsonResponse;
 try {
 ArrayList<String> temp = new ArrayList<String>();
 jsonResponse = new JSONObject(json);
 JSONArray movies = jsonResponse.getJSONArray("abridged_cast");
 for(int i=0;i<movies.length();i++){
 JSONObject movie = movies.getJSONObject(i);
 JSONArray characters = movie.getJSONArray("characters");
 for(int j=0;j<characters.length();j++){
 temp.add(characters.getString(j));
 }
 }
 Toast.makeText(this, "Json: "+temp, Toast.LENGTH_LONG).show();
 } catch (JSONException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }

checked it :)

answered Jun 16, 2013 at 19:52

Comments

1

Here is a better way for doing it. Hope this helps

protected void onPostExecute(String result) {
 Log.v(TAG + " result);
 if (!result.equals("")) {
 // Set up variables for API Call
 ArrayList<String> list = new ArrayList<String>();
 try {
 JSONArray jsonArray = new JSONArray(result);
 for (int i = 0; i < jsonArray.length(); i++) {
 list.add(jsonArray.get(i).toString());
 }//end for
 } catch (JSONException e) {
 Log.e(TAG, "onPostExecute > Try > JSONException => " + e);
 e.printStackTrace();
 }
 adapter = new ArrayAdapter<String>(ListViewData.this, android.R.layout.simple_list_item_1, android.R.id.text1, list);
 listView.setAdapter(adapter);
 listView.setOnItemClickListener(new OnItemClickListener() {
 @Override
 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
 // ListView Clicked item index
 int itemPosition = position;
 // ListView Clicked item value
 String itemValue = (String) listView.getItemAtPosition(position);
 // Show Alert
 Toast.makeText( ListViewData.this, "Position :" + itemPosition + " ListItem : " + itemValue, Toast.LENGTH_LONG).show();
 }
 });
 adapter.notifyDataSetChanged();
...
answered Dec 3, 2014 at 7:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.