Using GSON to json android converter

When trying to read and save a json envelope using GSON library using 2 lines below, the application crashes (see error log). What's wrong with parsing.

Type listType = new TypeToken<ArrayList<MRechercheResult>>() {}.getType();
        List<MRechercheResult>search_results  = new Gson().fromJson(envelop, listType);

      

Error log

04-21 11:16:59.971: E/AndroidRuntime(19633): FATAL EXCEPTION: main
04-21 11:16:59.971: E/AndroidRuntime(19633): com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY at line 1 column 178
04-21 11:16:59.971: E/AndroidRuntime(19633):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
04-21 11:16:59.971: E/AndroidRuntime(19633):    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
04-21 11:16:59.971: E/AndroidRuntime(19633):    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
04-21 11:16:59.971: E/AndroidRuntime(19633):    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
04-21 11:16:59.971: E/AndroidRuntime(19633):    at com.google.gson.Gson.fromJson(Gson.java:803)
04-21 11:16:59.971: E/AndroidRuntime(19633):    at com.google.gson.Gson.fromJson(Gson.java:768)
04-21 11:16:59.971: E/AndroidRuntime(19633):    at com.google.gson.Gson.fromJson(Gson.java:717)
04-21 11:16:59.971: E/AndroidRuntime(19633):    at waoo.app.fragment.RechercheResult.saveJson(RechercheResult.java:255)
04-21 11:16:59.971: E/AndroidRuntime(19633):    at waoo.app.fragment.RechercheResult$1$1.handleMessage(RechercheResult.java:218)
04-21 11:16:59.971: E/AndroidRuntime(19633):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-21 11:16:59.971: E/AndroidRuntime(19633):    at android.os.Looper.loop(Looper.java:137)
04-21 11:16:59.971: E/AndroidRuntime(19633):    at android.app.ActivityThread.main(ActivityThread.java:5306)
04-21 11:16:59.971: E/AndroidRuntime(19633):    at java.lang.reflect.Method.invokeNative(Native Method)
04-21 11:16:59.971: E/AndroidRuntime(19633):    at java.lang.reflect.Method.invoke(Method.java:511)
04-21 11:16:59.971: E/AndroidRuntime(19633):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
04-21 11:16:59.971: E/AndroidRuntime(19633):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
04-21 11:16:59.971: E/AndroidRuntime(19633):    at dalvik.system.NativeStart.main(Native Method)
04-21 11:16:59.971: E/AndroidRuntime(19633): Caused by: java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY at line 1 column 178
04-21 11:16:59.971: E/AndroidRuntime(19633):    at com.google.gson.stream.JsonReader.nextString(JsonReader.java:821)
04-21 11:16:59.971: E/AndroidRuntime(19633):    at com.google.gson.internal.bind.TypeAdapters$13.read(TypeAdapters.java:358)
04-21 11:16:59.971: E/AndroidRuntime(19633):    at com.google.gson.internal.bind.TypeAdapters$13.read(TypeAdapters.java:346)
04-21 11:16:59.971: E/AndroidRuntime(19633):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
04-21 11:16:59.971: E/AndroidRuntime(19633):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
04-21 11:16:59.971: E/AndroidRuntime(19633):    ... 16 more

      

Model

public class MRechercheResult {


    public String id_profil;
    public String id_customer;
    public String profil;
    public String profil_photo;
    public String email;
    public String agenda;

    public List<Activite> activite;

}

public class Activite {

    public String day;
    public String from;
    public String to;

}

      

You can find the json envelope here:

http://pastie.org/private/ipirfc9pgftd8lmjaoipog

+3


source to share


4 answers


Hope this helps. Using gson you need to be careful about the variable name and type :)



public class MRechercheResult {


        public String id_profil;
        public String id_customer;
        public String profil;
        public String profil_photo;
        public String email;
        public Agenda[] agenda;

        public Activite[] activite;

    }

    public class Activite {

        public String day;
        public String from;
        public String to;

    }

        public class Agenda {
        String day;
        String from;
        String to;
    }

}

      

+1


source


agenda is an array of another json object, not a string. change your agenda type in the MRechercheResult class. use http://www.jsonschema2pojo.org/ to generate pojo classes for json string



+1


source


Your json doesn't start with Object. So, I am doing the following steps:

First : wrap your first Array in an Object (I named "example")

{
    "example": [
    {  
      "id_profil":"2",
      "id_customer":"3",
      "profil":"Spartan GYM",
      "profil_photo":"https:\/\/waoo.com\/img\/profil_pic\/job_provider-54b791f46d7b53.09461979.png",
      "email":"",
      "agenda":[  
            ...
      ],
      "activite":[  
      ]  
    },
    {
        ....
    }
    ]
}

      

Second , define the Full.java class:

public class Full {
    public List<Example> example = new ArrayList<>();

    class Example {
        public String id_profil;
        public String id_customer;
        public String profil;
        public String profil_photo;
        public String email;
        public List<Agenda> agenda = new ArrayList<Agenda>();
        public List<Activite> activite = new ArrayList<Activite>();

        class Agenda {

            public String day;
            public String from;
            public String to;

        }

        class Activite {

            public String id_profil_style_activite;
            public String activite_name;

        }
    }
}

      

Then : define the get data class with asynctask:

private class ParseByGson extends AsyncTask<String,Void,Full> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Full doInBackground(String... params) {
        Full fullContents = null;
        try {
            URL url=new URL(params[0]);
            InputStreamReader reader=new InputStreamReader(url.openStream(),"UTF-8");
            fullContents=new Gson().fromJson(reader,Full.class);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fullContents;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(Full results) {
        super.onPostExecute(results);
        Log.e(">>",results.example.get(0).id_customer+"--"+results.example.get(0).activite.get(0).activite_name+"--"+results.example.get(0).agenda.get(0).day+"--");
    }
}

      

Finally , use it:

ParseByGson parseByGson = new ParseByGson();
parseByGson.execute(url_of_your_json);

      

+1


source


You are missing the Backslash escape in the second url.

it should be https:\/\/

insteadhttps:\/\

0


source







All Articles