尝试使用下面的2行使用GSON库读取和存储json信封时,应用程序崩溃(请参阅错误日志)。解析有什么问题。
Type listType = new TypeToken<ArrayList<MRechercheResult>>() {}.getType();
List<MRechercheResult>search_results = new Gson().fromJson(envelop, listType);
错误日志
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
模型
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;
}
你可以在这里找到json信封:
答案 0 :(得分:1)
议程是另一个json对象的数组而不是字符串..在MRechercheResult类中更改您的议程类型.. 使用http://www.jsonschema2pojo.org/为json string
生成pojo类答案 1 :(得分:1)
希望这会有所帮助..使用gson你需要注意变量名称和类型:)
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;
}
}
答案 2 :(得分:1)
您的json不以Object开头。所以,我做了以下步骤:
首先:使用Object包装您的第一个Array对象(我命名为&#34;示例&#34;)
{
"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":[
]
},
{
....
}
]
}
第二次:定义Full.java类:
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;
}
}
}
然后:通过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+"--");
}
}
最后,请使用它:
ParseByGson parseByGson = new ParseByGson();
parseByGson.execute(url_of_your_json);
答案 3 :(得分:0)
您在第二个网址中缺少反斜杠转义符。
应该是https:\/\/
而不是https:\/\