gpt4 book ai didi

android - 将 Gson 转换为对象?

转载 作者:行者123 更新时间:2023-11-29 15:57:40 26 4
gpt4 key购买 nike

我正在尝试使用 Gson 与 Web 服务的 JSON 返回一起使用。即 JSON 返回此格式:

{
"cod":999,
"msg":"User Data.",
"return":{
"ID":"74",
"name":"FERNANDO PAIVA",
"devices":[]
}
}

当我尝试使用 gson.fromJson(json_return, MyObject.class 时,它不起作用并抛出异常。我遵循此用户指南:https://sites.google.com/site/gson/gson-user-guide#TOC-Using-Gson

我该怎么做?

我正在尝试这个。

public class User implements Serializable{
private static final long serialVersionUID = 1L;

@SerializedName("ID")
private Long id;

private String name;

//get and set
}

/** get informations of web service */
public static String get(String url){
String s = null;

try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(new HttpGet(url));
HttpEntity entity = httpResponse.getEntity();
if(entity != null){
s = EntityUtils.toString(entity);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}


/** return object User */
public User getObjectById(){
String url = "mydomain.com.br/script.php?email=myemail@domain.com";;
String response = HttpClient.get(url);
Gson gson = new Gson();
ResponseUser ru = gson.fromJson(response, ResponseUser.class);
User u = ru.getUser();

return u;
}


public class ResponseUser {

@SerializedName("return")
private User user;

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}
}

异常

11-19 12:56:23.281: E/AndroidRuntime(1481): FATAL EXCEPTION: main
11-19 12:56:23.281: E/AndroidRuntime(1481): Process: br.com.package.myapp, PID: 1481
11-19 12:56:23.281: E/AndroidRuntime(1481): java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.package.myapp/br.com.package.myapp.act.MainActivity}: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1
11-19 12:56:23.281: E/AndroidRuntime(1481): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
11-19 12:56:23.281: E/AndroidRuntime(1481): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
11-19 12:56:23.281: E/AndroidRuntime(1481): at android.app.ActivityThread.access$800(ActivityThread.java:135)
11-19 12:56:23.281: E/AndroidRuntime(1481): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
11-19 12:56:23.281: E/AndroidRuntime(1481): at android.os.Handler.dispatchMessage(Handler.java:102)
11-19 12:56:23.281: E/AndroidRuntime(1481): at android.os.Looper.loop(Looper.java:136)
11-19 12:56:23.281: E/AndroidRuntime(1481): at android.app.ActivityThread.main(ActivityThread.java:5017)
11-19 12:56:23.281: E/AndroidRuntime(1481): at java.lang.reflect.Method.invokeNative(Native Method)
11-19 12:56:23.281: E/AndroidRuntime(1481): at java.lang.reflect.Method.invoke(Method.java:515)
11-19 12:56:23.281: E/AndroidRuntime(1481): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-19 12:56:23.281: E/AndroidRuntime(1481): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-19 12:56:23.281: E/AndroidRuntime(1481): at dalvik.system.NativeStart.main(Native Method)
11-19 12:56:23.281: E/AndroidRuntime(1481): Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1
11-19 12:56:23.281: E/AndroidRuntime(1481): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
11-19 12:56:23.281: E/AndroidRuntime(1481): at com.google.gson.Gson.fromJson(Gson.java:803)
11-19 12:56:23.281: E/AndroidRuntime(1481): at com.google.gson.Gson.fromJson(Gson.java:768)
11-19 12:56:23.281: E/AndroidRuntime(1481): at com.google.gson.Gson.fromJson(Gson.java:717)
11-19 12:56:23.281: E/AndroidRuntime(1481): at com.google.gson.Gson.fromJson(Gson.java:689)
11-19 12:56:23.281: E/AndroidRuntime(1481): at br.com.package.myapp.dao.UsuarioDAO.getObjectById(UsuarioDAO.java:46)
11-19 12:56:23.281: E/AndroidRuntime(1481): at br.com.package.myapp.act.MainActivity.onCreate(MainActivity.java:27)
11-19 12:56:23.281: E/AndroidRuntime(1481): at android.app.Activity.performCreate(Activity.java:5231)
11-19 12:56:23.281: E/AndroidRuntime(1481): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-19 12:56:23.281: E/AndroidRuntime(1481): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
11-19 12:56:23.281: E/AndroidRuntime(1481): ... 11 more
11-19 12:56:23.281: E/AndroidRuntime(1481): Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1
11-19 12:56:23.281: E/AndroidRuntime(1481): at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:374)
11-19 12:56:23.281: E/AndroidRuntime(1481): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:165)
11-19 12:56:23.281: E/AndroidRuntime(1481): ... 20 more

最佳答案

try {
JSONObject responseObj = new JSONObject(content);
Gson gson = new Gson();
User user = gson.fromJson(responseObj.getJSONObject("return"), User.class);
} catch (JSONException e) {
// catch here
}

假设用户类设置正确

关于android - 将 Gson 转换为对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27022199/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com