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...

1
  • is it your complete JSON or just an entry? Commented Jun 27, 2014 at 11:48

1 Answer 1

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

7 Comments

@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??
@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.
@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??
@dheeraj92 Done! ;-) Now, plz refer to the edited code. It should work fine in your case, I hope.
@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??
|

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.