How to parse json parsing Using GSON in android

you could try reading the gson value like this:

try {
      AssetManager assetManager = getAssets();
  InputStream ims = assetManager.open("file.txt");

  Gson gson = new Gson();
  Reader reader = new InputStreamReader(ims);

  GsonParse gsonObj = gson.fromJson(reader, GsonParse.class);

     }catch(IOException e) {
       e.printStackTrace();
 }

Assuming that you are just receiving this one block and not a list. And also this data is currently in a file in the assets folder. You can change it to the stream you want to read it from.

The class you use should look like:

GsonParse.class

public class GsonParse {
 @SerializedName("count")
 private String count;

 @SerializedName("colbreak")
 private String colbreak;

 @SerializedName("name")
 private String name;

 @SerializedName("score")
 private String score;

 @SerializedName("Words")
 private List<Words> mWords = new ArrayList<Words>();

 @SerializedName("seek")
 private String seek;

public String getCount() {
    return count;
}

public void setCount(String count) {
    this.count = count;
}

public String getColbreak() {
    return colbreak;
}

public void setColbreak(String colbreak) {
    this.colbreak = colbreak;
}

private String getName() {
    return name;
}

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

public String getScore() {
    return score;
}

public void setScore(String score) {
    this.score = score;
}

public List<Words> getmWords() {
    return mWords;
}

public void setmWords(List<Words> mWords) {
    this.mWords = mWords;
}

public String getSeek() {
    return seek;
}

public void setSeek(String seek) {
    this.seek = seek;
}
}

Words.class

public class Words {
@SerializedName(value ="count")
private String count;
@SerializedName(value="word")
private String word;
@SerializedName(value="score")
private String name;
@SerializedName(value="Words")
private String words;
@SerializedName(value="seek")
private String seek;

    public String getCount() {
    return count;
}
public void setCount(String count) {
    this.count = count;
}
public String getWord() {
    return word;
}
public void setWord(String word) {
    this.word = word;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getWords() {
    return words;
}
public void setWords(String words) {
    this.words = words;
}
public String getSeek() {
    return seek;
}
public void setSeek(String seek) {
    this.seek = seek;
}
}

there is a parameter missing in words.class, you could add it .

GSON does not directly support UTF-8 characters. so when receiving the response using http, you will have to convert that to utf-8 form in the response of http itself.

you could try using:

String jsonString = new Gson().toJson(objectToEncode);
byte[] utf8JsonString = jsonString.getBytes("UTF8");
responseToClient.write(utf8JsonString, 0, utf8JsonString.Length);

Hello use below gradle lib

compile 'com.google.code.gson:gson:2.2.4'

Json Class

import java.util.List;
public class GsonParse{

/**
 * count : 12
 * colbreak : 1
 * name : unary rels
 * score : 9090
 * Words : [{"count":6,"word":"prp_għaċ-","name":"prp_għaċ-","score":9.1,"Words":"kol","seek":2231297}]
 * seek : 0
 */

private String count;
private int colbreak;
private String name;
private String score;
private int seek;
/**
 * count : 6
 * word : prp_għaċ-
 * name : prp_għaċ-
 * score : 9.1
 * Words : kol
 * seek : 2231297
 */

private List<WordsBean> Words;

public String getCount() {
    return count;
}

public void setCount(String count) {
    this.count = count;
}

public int getColbreak() {
    return colbreak;
}

public void setColbreak(int colbreak) {
    this.colbreak = colbreak;
}

public String getName() {
    return name;
}

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

public String getScore() {
    return score;
}

public void setScore(String score) {
    this.score = score;
}

public int getSeek() {
    return seek;
}

public void setSeek(int seek) {
    this.seek = seek;
}

public List<WordsBean> getWords() {
    return Words;
}

public void setWords(List<WordsBean> Words) {
    this.Words = Words;
}

public static class WordsBean {
    private int count;
    private String word;
    private String name;
    private double score;
    private String Words;
    private int seek;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public String getWord() {
        return word;
    }

    public void setWord(String word) {
        this.word = word;
    }

    public String getName() {
        return name;
    }

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

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    public String getWords() {
        return Words;
    }

    public void setWords(String Words) {
        this.Words = Words;
    }

    public int getSeek() {
        return seek;
    }

    public void setSeek(int seek) {
        this.seek = seek;
    }
}

}

Calling API put response in below code and Retrive Data

GsonParse gsonparse = gson.fromJson(response, GsonParse.class);
//gsonparse.getWords() // It will returns list of Words
//Also do loop and get more data using data
gsonparse.getColbreak();
gsonparse.getSeek();
for (GsonParse.WordsBean data:gsonparse.getWords())
{

    data.getName();

}

hope it will help to you..


Replace

@SerializedName("name") public String count; with

@SerializedName("name")
public String name;

I guess the issue is you are using public String count; for both @SerializedName("count") and @SerializedName("name")

Thanks.


Try this:

JSONArray jsonarray = jsonObject.getJSONArray("responseData");
Type listType = new TypeToken<ArrayList<AllUsers>>(){}.getType();
List<AllUsers> allUserses = 
    new GsonBuilder().create().fromJson(jsonarray.toString(), listType);

for(AllUsers user: allUserses){
         allUsersDao.insertOrReplace(user);
}