怎样讲JSON反序列化为复杂实体类
发布网友
发布时间:2022-04-26 18:12
我来回答
共2个回答
热心网友
时间:2022-04-09 17:38
java的话你可以使用Gson这个包
下面是“演示”代码
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
public class GsonTest {
public static void main(String[] args) {
Gson g = new Gson();
Bean b = new Bean();
b.setString_("字符串");
b.setInt_(0);
b.setDouble_(1.5);
b.setBoolean_(false);
ArrayList<String> l = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
l.add(String.valueOf(i));
}
b.setList_(l);
System.out.println("这个Bean重写了equals方法,待会比较用");
System.out.println("转换前的Bean" + b);
System.out.println("前面的代码不用管,下面是json");
String json = g.toJson(b);
System.out.println(json);
System.out.println("下面是将json转成对象");
Bean b2 = g.fromJson(json, Bean.class);
System.out.println("转换回来的:" + b2);
System.out.println("比较两者的内容是否相等:" + b.equals(b2));
}
}
class Bean {
private String string_;
private boolean boolean_;
private double double_;
private int int_;
private List<String> list_;
public List<String> getList_() {
return list_;
}
public void setList_(List<String> list_) {
this.list_ = list_;
}
public String getString_() {
return string_;
}
public boolean isBoolean_() {
return boolean_;
}
public double getDouble_() {
return double_;
}
public int getInt_() {
return int_;
}
public void setString_(String string_) {
this.string_ = string_;
}
public void setBoolean_(boolean boolean_) {
this.boolean_ = boolean_;
}
public void setDouble_(double double_) {
this.double_ = double_;
}
public void setInt_(int int_) {
this.int_ = int_;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (boolean_ ? 1231 : 1237);
long temp;
temp = Double.doubleToLongBits(double_);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + int_;
result = prime * result + ((list_ == null) ? 0 : list_.hashCode());
result = prime * result + ((string_ == null) ? 0 : string_.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Bean other = (Bean) obj;
if (boolean_ != other.boolean_)
return false;
if (Double.doubleToLongBits(double_) != Double
.doubleToLongBits(other.double_))
return false;
if (int_ != other.int_)
return false;
if (list_ == null) {
if (other.list_ != null)
return false;
} else if (!list_.equals(other.list_))
return false;
if (string_ == null) {
if (other.string_ != null)
return false;
} else if (!string_.equals(other.string_))
return false;
return true;
}
@Override
public String toString() {
return "Bean [string_=" + string_ + ", boolean_=" + boolean_
+ ", double_=" + double_ + ", int_=" + int_ + ", list_="
+ list_ + "]";
}
}
热心网友
时间:2022-04-09 18:56
C# code
123456789101112131415161718192021222324252627282930313233343536373839{ "QUES_JSON": [ { "chapter": true, "code": "一", "name": "一般情况", "type": "part", "values": { } }, { "chapter": false, "code": "1.1", "name": "性别", "type": "radio", "valueDict": [ { "code": "1", "name": "男性", "type": "", "valueDict": [ ] }, { "code": "2", "name": "女性", "type": "", "valueDict": [ ] } ], "values": { "2": { "appends": { }, "code": "2", "value": "女性" } } } ]}这个应该如何 反序列化的实体类啊