I am using GSON
for parse Json data. My Json data is below:
{
"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<Words> mWords = new ArrayList<Words>();
@SerializedName("seek")
public String seek;
}
I am using below method to parse this JSON data.
public static <T> ArrayList<T> JsonParse(T t, String response) {
// convert String into InputStream
InputStream in = new ByteArrayInputStream(response.getBytes());
JsonReader reader;
ArrayList<T> lcs = new ArrayList<T>();
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<T>) lcs;
}
I am facing below 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
Replace
@SerializedName("name") public String count;
withI guess the issue is you are using
public String count;
for both@SerializedName("count")
and@SerializedName("name")
Thanks.
Hello use below gradle lib
Json Class
}
Calling API put response in below code and Retrive Data
hope it will help to you..
There are many thing missing in your code..
Please look at below code and try this code..
For more detail look at this example
Try this:
you could try reading the gson value like this:
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
Words.class
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: