如何在没有键名的情况下获取json数组数据

时间:2019-06-24 11:09:49

标签: android json

这是我的json数据,我想在顶部标题中获取first_movie名称,并在其下获取其余数据,以及其他工作也是如此

{
"actors": [
{
"name": "Amitabh Bachchan",
"age": 76,
"bollywood": true,
"img": "https://upload.wikimedia.org/wikipedia/commons/thumb/7/73/Amitabh_Bachchan_2014.jpg/220px-Amitabh_Bachchan_2014.jpg",
"notablework": [
{
"first_movie": "Saat Hindustani"
},
"Anand(1971)",
"Zanjeer(1973)",
"Sholay(1975)",
"Don(1978)",
"Agneepath(1990)",
"Black(2005)",
"Paa(2009)",
"Piku(2016)"
],
"other_work": [
"Politics",
"Television appearances",
"Voice-acting",
"Humanitarian causes",
"Business investments"
]
},
{
"name": "Salman Khan",
"age": 53,
"bollywood": true,
"img": "https://upload.wikimedia.org/wikipedia/commons/thumb/8/86/Salman_Khan_at_Renault_Star_Guild_Awards.jpg/220px-Salman_Khan_at_Renault_Star_Guild_Awards.jpg",
"notablework": [
{
"first_movie": "Biwi Ho To Aisi"
},
" Biwi Ho To Aisi (1988)",
"Maine Pyar Kiya (1989)",
" Hum Aapke Hain Koun..! (1994)",
" Karan Arjun (1995)"
],
"other_work": [
"Television",
"Brand endorsements"
]

},click here to view my design that i want to show this data

1 个答案:

答案 0 :(得分:0)

据我了解,我认为您共享了两个json对象。

您的JSON

{  
       "actors":[  
          {  
             "name":"Amitabh Bachchan",
             "age":76,
             "bollywood":true,
             "img":"https://upload.wikimedia.org/wikipedia/commons/thumb/7/73/Amitabh_Bachchan_2014.jpg/220px-Amitabh_Bachchan_2014.jpg",
             "notablework":[  
                {  
                   "first_movie":"Saat Hindustani"
                },
                "Anand(1971)",
                "Zanjeer(1973)",
                "Sholay(1975)",
                "Don(1978)",
                "Agneepath(1990)",
                "Black(2005)",
                "Paa(2009)",
                "Piku(2016)"
             ],
             "other_work":[  
                "Politics",
                "Television appearances",
                "Voice-acting",
                "Humanitarian causes",
                "Business investments"
             ]
          },
          {  
             "name":"Salman Khan",
             "age":53,
             "bollywood":true,
             "img":"https://upload.wikimedia.org/wikipedia/commons/thumb/8/86/Salman_Khan_at_Renault_Star_Guild_Awards.jpg/220px-Salman_Khan_at_Renault_Star_Guild_Awards.jpg",
             "notablework":[  
                {  
                   "first_movie":"Biwi Ho To Aisi"
                },
                " Biwi Ho To Aisi (1988)",
                "Maine Pyar Kiya (1989)",
                " Hum Aapke Hain Koun..! (1994)",
                " Karan Arjun (1995)"
             ],
             "other_work":[  
                "Television",
                "Brand endorsements"
             ]
          }
       ]
    }

在这里我们处理以上数据

     JSONObject json_obj = new JSONObject(response);
     //we have the array named hero inside the object
     //so here we are getting that json array 
     JSONArray actorsArray = json_obj.getJSONArray("actors");

    //now looping through all the elements of the json array 
    for (int i = 0; i < actorsArray.length(); i++) {

       //getting the json object of the particular index inside the array
       JSONObject actorsObject = actorsArray.getJSONObject(i);

       String actorsName = actorsObject.getString("name");

       //Here you can get same like your remaining string data by using json key -> age,bollywood,img

       //now we can process notablework object

       JSONArray notableworkArray = actorsObject.getJSONArray("notablework");
       // now we need another loop for get notable data

          for (int j = 0; j < notableworkArray.length(); j++) {

               if(j==0) {
                 JSONObject notableworObject = notableworkArray.getJSONObject(0);
                 String first_movie = notableworObject.getString("first_movie");

               }

          }

    }