关于android:如何在Retrofit 2中解析json以下内容?

How to parse below json in Retrofit 2?

{
"状态":是的,
" message":" Welcome jaymin",
"数据": {
" id":1
" name":" jaymin",
" email":" [email protected]",
" mobile":" 123456"
}
}


您可以添加GsonFactory或JacksonFactory来创建改造服务
并可以使用此链接http://www.jsonschema2pojo.org/
创建一个pojo类,您可以通过该类来解析数据。我已经将您的JSON转换为Gson格式的Java类,您可以在android中使用它来解析数据。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
   -----------------------------------com.example.Data.java-----------------------
   ------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Data {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("email")
@Expose
private String email;
@SerializedName("mobile")
@Expose
private String mobile;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getMobile() {
return mobile;
}

public void setMobile(String mobile) {
this.mobile = mobile;
}

}
-----------------------------------com.example.FollowersResponse.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class FollowersResponse {

@SerializedName("status")
@Expose
private Boolean status;
@SerializedName("message")
@Expose
private String message;
@SerializedName("data")
@Expose
private Data data;

public Boolean getStatus() {
return status;
}

public void setStatus(Boolean status) {
this.status = status;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public Data getData() {
return data;
}

public void setData(Data data) {
this.data = data;
}

}


要将字符串短语化为JSONObject,请执行以下操作

1
2
String jsonString = '{"status": true,"message":"Welcome jaymin","data": {"id": 1,"name":"jaymin","email":"[email protected]","mobile":"123456" } }'
JSONObject jsonObj = new JSONObject(jsonString)

然后您可以像这样从JSONObject检索值

1
String message = jsonObj.get("message") //Message ="Welcome jaymin"