Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse json parsing Using GSON in android

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
like image 987
dipali Avatar asked Mar 31 '14 04:03

dipali


People also ask

What is Gson and JSON in Android?

On the other hand, GSON is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. GSON can work with arbitrary Java objects including pre-existing objects that you do not have source code of.

How do I parse JSON array in Kotlin?

You can use JSON to Kotlin Data class converter plugin in Android Studio for JSON mapping to POJO classes (kotlin data class). This plugin will annotate your Kotlin data class according to JSON. Then you can use GSON converter to convert JSON to Kotlin. If you want to parse json manually.


1 Answers

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);
}
like image 97
rahul gupta Avatar answered Sep 19 '22 19:09

rahul gupta