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();
}
String value = (String) jsonObject. get("key_name"); Just like other element retrieve the json array using the get() method into the JSONArray object.
Android JSON Parser Tutorial JSON (Javascript Object Notation) is a programming language . It is minimal, textual, and a subset of JavaScript. It is an alternative to XML. Android provides support to parse the JSON object and array.
We can also add a JSONArray to JSONObject. We need to add a few items to an ArrayList first and pass this list to the put() method of JSONArray class and finally add this array to JSONObject using the put() method.
JSONArray objects have a function getJSONObject(int index) , you can loop through all of the JSONObjects by writing a simple for-loop: JSONArray array; for(int n = 0; n < array. length(); n++) { JSONObject object = array. getJSONObject(n); // do some stuff.... }
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).
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With