Cómo analizar json analizar usando GSON en android

Estoy usando GSON para analizar datos de Json. Los datos de mi Json están a continuación:

 { "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 } 

GsonParse.java

 public class GsonParse { @SerializedName("count") public String count; @SerializedName("colbreak") public String colbreak; @SerializedName("name") public String count; @SerializedName("score") public String score; @SerializedName("Words") public List mWords = new ArrayList(); @SerializedName("seek") public String seek; } 

Estoy usando el método siguiente para analizar estos datos JSON.

 public static  ArrayList JsonParse(T t, String response) { // convert String into InputStream InputStream in = new ByteArrayInputStream(response.getBytes()); JsonReader reader; ArrayList lcs = new ArrayList(); try { reader = new JsonReader(new InputStreamReader(in, "UTF-8")); Gson gson = new Gson(); reader.beginObject(); while (reader.hasNext()) { T cse = (T) gson.fromJson(reader, t.getClass()); lcs.add(cse); } reader.endObject(); /* * reader.nextName(); reader.nextString(); reader.nextName(); * reader.nextString(); */ reader.close(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return (ArrayList) lcs; } 

Me estoy enfrentando el siguiente error

 03-31 10:14:26.968: E/AndroidRuntime(18578): com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was NAME at line 1 column 73 

podrías intentar leer el valor gson así:

 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(); } 

Asumiendo que solo estás recibiendo este bloque y no una lista. Y también estos datos se encuentran actualmente en un archivo en la carpeta de activos. Puede cambiarlo a la secuencia desde la que desea leerlo.

La clase que use debe verse así:

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 mWords = new ArrayList(); @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 getmWords() { return mWords; } public void setmWords(List 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; } } 

falta un parámetro en words.class, podrías agregarlo.

GSON no admite directamente caracteres UTF-8. así que cuando recibas la respuesta usando http, tendrás que convertir eso a formato utf-8 en la respuesta de http.

puedes intentar usar:

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

Hola, use debajo de lib libre

 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 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 getWords() { return Words; } public void setWords(List 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; } } 

}

La API llamante pone la respuesta en el código a continuación y 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(); } 

Espero que te ayude ..

Reemplazar

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

 @SerializedName("name") public String name; 

Supongo que el problema es que estás usando public String count; para ambos @SerializedName("count") y @SerializedName("name")

Gracias.

Prueba esto:

 JSONArray jsonarray = jsonObject.getJSONArray("responseData"); Type listType = new TypeToken>(){}.getType(); List allUserses = new GsonBuilder().create().fromJson(jsonarray.toString(), listType); for(AllUsers user: allUserses){ allUsersDao.insertOrReplace(user); } 

Hay muchas cosas que faltan en tu código …

Mire el código a continuación e intente con este código.

 import java.util.List; public class GsonParse { public GsonParse() { } public String count = ""; public String colbreak = ""; public String name = ""; public String score = ""; public int seek = 0; List Words; 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; } 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 getWords() { return Words; } public void setWords(List words) { Words = words; } public class WordsData { public WordsData() { } public int count = 0; public String word = ""; public String name = ""; public double score = 0.0; public String Words = ""; public int seek = 0; 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) { Words = words; } public int getSeek() { return seek; } public void setSeek(int seek) { this.seek = seek; } } } 

Para más detalles mira este ejemplo