1

I want to parse a JSON Response through GSON in android, for example

 {
  "adult": false,
  "budget": 63000000,
  "spoken_languages": [
    {
      "iso_639_1": "en",
      "name": "English"
    }
  ],
}     

first class for this

public Detail parseDetailResponse(InputStream json) {
Gson gson = new Gson();
Reader reader=new InputStreamReader(json);
Detail handle = gson.fromJson(reader, Detail.class);
return handle;

}

class for parsing this

public class Detail implements Serializable{
private static final long serialVersionUID = -6814886315783830255L;

@SerializedName("adult")
public boolean Adult;
    @SerializedName("spoken_languages")
public lang[] Languages;
   }

My lang class

public class lang implements Serializable{
private static final long serialVersionUID = -6814886315783830255L;
@SerializedName("name")
public String Name;
}

now i want the value of lang.name, but it gives null pointer exception.. pls help How i am getting this value...

Lavekush Agrawal
  • 6,040
  • 7
  • 52
  • 85
dheerajraaj
  • 124
  • 13

1 Answers1

1

Try this, though it's not an elegant way. But at least, it get things done.

The problem of your previous code is that GSON thought spoken_languages in the JSON string to be an array, so you have to create something at like an array to be reflected. Here I choose an ArrayList. Hope that your problem can be settled.

import com.google.gson.GsonBuilder;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class SimpleTest {
    public static void main(String[] args) {
        String json = "{\n" +
                "  \"adult\": false,\n" +
                "  \"budget\": 63000000,\n" +
                "  \"spoken_languages\": [\n" +
                "    {\n" +
                "      \"iso_639_1\": \"en\",\n" +
                "      \"name\": \"English\"\n" +
                "    }\n" +
                "  ]\n" +
                "}     ";

        System.out.println(new SimpleTest().parseDetailResponse(new ByteArrayInputStream(json.getBytes())));
    }

    public Detail parseDetailResponse(InputStream json) {
        return new GsonBuilder().create().fromJson(new InputStreamReader(json), Detail.class);
    }

    class Detail {
        private boolean adult;
        private long budget;
        private ArrayList<SpokenLanguages> spoken_languages;

        public Detail() {

        }

        @Override
        public String toString() {
            return "DAO{" +
                    "adult=" + adult +
                    ", budget=" + budget +
                    ", spoken_languages=" + spoken_languages +
                    '}';
        }

        public boolean isAdult() {
            return adult;
        }

        public void setAdult(boolean adult) {
            this.adult = adult;
        }

        public long getBudget() {
            return budget;
        }

        public void setBudget(long budget) {
            this.budget = budget;
        }

        public List<SpokenLanguages> getSpoken_languages() {
            return spoken_languages;
        }

        public void setSpoken_languages(ArrayList<SpokenLanguages> spoken_languages) {
            this.spoken_languages = spoken_languages;
        }
    }

    class SpokenLanguages {
        private String name;
        private String iso_639_1;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getIso_639_1() {
            return iso_639_1;
        }

        public void setIso_639_1(String iso_639_1) {
            this.iso_639_1 = iso_639_1;
        }

        @Override
        public String toString() {
            return "SpokenLanguages{" +
                    "name='" + name + '\'' +
                    ", iso_639_1='" + iso_639_1 + '\'' +
                    '}';
        }
    }
}
Xiao Liang
  • 706
  • 5
  • 13
  • @lzzy Leung thanks for your response... but sir what is the return statement in the main method of your SimpleTest class like in my code i am returning object of the Detail class?? – dheerajraaj Jun 27 '14 at 11:48
  • @dheeraj92 Ha, the returning value in this statement is a `String`. It reflects the Object from the JSON String by calling new `GsonBuilder().create().fromJson(json, DAO.class)` and then, I call the `toString()` method of the object, which yields an String representing it. I call `System.out.println()` to show that GSON has successfully reflected the object. – Xiao Liang Jun 27 '14 at 11:53
  • @lzzy Leung you made toString method in the respective class and prints the value but i want these value in other class... and if the return is string then how i will get those values... pls write modified parseDetailResponse() method which is mentioned in my question?? – dheerajraaj Jun 27 '14 at 12:03
  • @dheeraj92 Done! ;-) Now, plz refer to the edited code. It should work fine in your case, I hope. – Xiao Liang Jun 27 '14 at 12:11
  • @lzzy Leung thanks sir, by Detail.getAdult() and Detail.getBudget() gives the value but now i want the only value of "name" of SpokenLanguage(), how i will get it?? – dheerajraaj Jun 27 '14 at 12:32
  • @lzzy Leung thanks sir,, finally got it.. and sorry. – dheerajraaj Jun 27 '14 at 12:45
  • @lzzy Leung sorry, i am not able to upvote any answer because it requires 15 reputation.. i am new on this site .. – dheerajraaj Jun 29 '14 at 05:39