我有这个Json内容:
{
"people":[
{
"name":"test1",
"sirname":"test2",
"details":{
"social_no":1234567,
"creadit_card_no":34582342309
}
},
{
"name":"test3",
"sirname":"test4",
"details":{
"social_no":12345679,
"creadit_card_no":345823423090
}
}
]
}
根据逻辑,这个Json应该有3个POJO类:一个将保存People,People对象和Details对象列表的类。
现在我的问题是,是否有可能使用杰克逊将这个Json反序列化,或者如果不能与杰克逊一起使用GSON库反序列化?一个包含People列表,另一个包含例如Human
类,具有以下结构:
public class Human{
String name;
String sirname;
String social_no;
String creadit_card_no;
//..getters and setters
//should correspond with this json fragment:
// {
// "name":"test1",
// "sirname":"test2",
// "details":{
// "social_no":1234567,
// "creadit_card_no":34582342309
// }
}
}
所以,如果可以,我该怎么做?
更新
我的实际json结构与此处给出的示例不同,因此here is the original json
所以我自己创建了TypeAdapter
,这是这个类的代码:
public class PlanTypeAdapter extends TypeAdapter<Plan> {
private final String TAG = PlanTypeAdapter.class.getSimpleName();
@Override
public void write(JsonWriter out, Plan value) throws IOException {
Log.d(TAG, "WRITE");
}
@Override
public Plan read(JsonReader reader) throws IOException {
Log.d(TAG, "READ");
Plan plan = new Plan();
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return null;
}
reader.setLenient(false);
while (reader.hasNext()) {
Log.d(TAG, "PATH: " + reader.getPath());
Log.d(TAG, "PEEK: " + reader.peek());
if (reader.peek() == JsonToken.BEGIN_OBJECT) {
Log.d(TAG, "BEGIN object, path: " + reader.getPath());
reader.beginObject();
} else if (reader.peek() == JsonToken.NULL) {
Log.d(TAG, "NULL");
reader.skipValue();
} else if (reader.peek() == JsonToken.END_ARRAY) {
Log.d(TAG, "END ARRAY");
if (reader.getPath().contains("retailer")) {
reader.endObject();
} else {
reader.endArray();
}
} else if (reader.peek() == JsonToken.END_OBJECT) {
reader.endObject();
Log.d(TAG, "END object, path: " + reader.getPath());
} else if (reader.peek() == JsonToken.NUMBER) {
Log.d(TAG, "NUMBER " + reader.getPath());
} else if (reader.peek() == JsonToken.BOOLEAN) {
Log.d(TAG, "BOOLEAN " + reader.getPath());
} else if (reader.peek() == JsonToken.NAME) {
switch (reader.nextName()) {
case "retailer":
reader.beginObject();
Log.d(TAG, "RET");
break;
case "national_plan":
reader.beginObject();
Log.d(TAG, "NPlan");
break;
case "name":
if (reader.getPath().contains("retailer")) {
plan.setRetailer_name(reader.nextString());
reader.skipValue();
reader.skipValue();
reader.endObject();
} else {
reader.skipValue();
}
break;
case "contract_end":
plan.setContract_end(reader.nextString());
break;
case "data_level_gb":
plan.setData_level_gb(reader.nextString());
break;
case "data_level_id":
plan.setData_level_id(reader.nextInt());
break;
case "days_to_end":
plan.setDays_to_switch(reader.nextInt());
break;
case "direct_from_operator":
plan.setDirect_from_operator(reader.nextBoolean());
break;
case "calculation_amount":
plan.setCalculationAmount(reader.nextDouble());
break;
case "network_generation_name":
plan.setNetwork_generation_(reader.nextString());
break;
case "partner_plan_id":
plan.setPartner_plan_id(reader.nextString());
break;
case "payment_level":
plan.setPayment_level(reader.nextString());
break;
case "payment_level_id":
plan.setPayment_level_id(reader.nextInt());
break;
case "roaming_amount":
plan.setRoaming_amount(reader.nextDouble());
break;
case "savings_amount":
plan.setSavings_amount(reader.nextDouble());
break;
case "savings_avg":
plan.setSavings_avg(reader.nextDouble());
break;
case "savings_percents":
plan.setSavings_percents(reader.nextInt());
break;
default:
Log.d(TAG, "DEFAULT " + reader.peek() + "");
reader.skipValue();
break;
}
} else {
reader.skipValue();
}
}
return plan;
}
}
答案 0 :(得分:1)
使用gson库可以使用不同的方法解析json。我会举两个例子。
方法1 - 编写自定义反序列化程序。此技术使用类来反序列化person对象。自定义反序列化器允许您使用json数据创建所需的任何对象。以下是执行此操作所需的类:
Group.java:
public class Group {
@SerializedName("people")
private List<Person> persons;
public List<Person> getPersons() {
return persons;
}
public void setPersons(List<Person> persons) {
this.persons = persons;
}
@Override
public String toString() {
String NEW_LINE = System.getProperty("line.separator");
StringBuilder sb = new StringBuilder(this.getClass().getName());
sb.append("{");
sb.append(NEW_LINE);
for(Person p : persons){
sb.append(p.toString());
}
sb.append("}");
return sb.toString();
}
}
GsonTest.java:
public class GsonTest {
public static void main(String[] args) {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Person.class, new PersonDeserializer());
Gson gson = gsonBuilder.create();
try {
JsonParser parser = new JsonParser();
Object obj = parser.parse(new FileReader("C://data.json"));
JsonObject jsonObject = (JsonObject) obj;
Group group = gson.fromJson(jsonObject, Group.class);
System.out.println(group.toString());
} catch (JsonIOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonSyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Person.java:
public class Person {
public Person(String name, String sirname, Long social_no, Long creadit_card_no) {
this.name = name;
this.sirname = sirname;
this.social_no = social_no;
this.creadit_card_no = creadit_card_no;
}
private String name;
private String sirname;
private Long social_no;
private Long creadit_card_no;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSirname() {
return sirname;
}
public void setSirname(String sirname) {
this.sirname = sirname;
}
public Long getSocial_no() {
return social_no;
}
public void setSocial_no(Long social_no) {
this.social_no = social_no;
}
public Long getCreadit_card_no() {
return creadit_card_no;
}
public void Long(Long creadit_card_no) {
this.creadit_card_no = creadit_card_no;
}
@Override
public String toString() {
String NEW_LINE = System.getProperty("line.separator");
StringBuilder sb = new StringBuilder(this.getClass().getName());
sb.append("{");
sb.append(NEW_LINE);
sb.append("name: ");
sb.append(name);
sb.append(NEW_LINE);
sb.append("sirname: ");
sb.append(sirname);
sb.append(NEW_LINE);
sb.append("social_no: ");
sb.append(social_no);
sb.append(NEW_LINE);
sb.append("creadit_card_no: ");
sb.append(creadit_card_no);
sb.append(NEW_LINE);
sb.append("}");
sb.append(NEW_LINE);
return sb.toString();
}
}
PersonDeserializer.java
public class PersonDeserializer implements JsonDeserializer<Person> {
public Person deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
String name = jsonObject.get("name").getAsString();
String sirname = jsonObject.get("sirname").getAsString();
JsonObject details = jsonObject.get("details").getAsJsonObject();
Long social_no = details.get("social_no").getAsLong();
Long creadit_card_no = details.get("creadit_card_no").getAsLong();
Person person = new Person(name, sirname, social_no, creadit_card_no );
return person;
}
}
方法2 - 使用JsonReader类来解析json数据。您不必使用此技术一次加载整个json文件。这是在资源有限的设备上解析大量数据的更好方法。如果json结构发生变化,这段代码将难以维护。我的示例代码受到了本文http://developer.android.com/reference/android/util/JsonReader.html的启发。使用上面的Person类和这个新的GsonTest类:
public class GsonTest {
List<Person> people = null;
public GsonTest() {
people = new ArrayList<Person>();
}
public static void main(String[] args) {
GsonTest gt = new GsonTest();
gt.doGson();
}
void doGson() {
try {
InputStream is = GsonTest.class.getResourceAsStream("data.json");
JsonReader jsonReader = new JsonReader(new InputStreamReader(is, "UTF-8"));
jsonReader.beginObject();
while (jsonReader.hasNext()) {
String name = jsonReader.nextName();
if (name.equals("people")) {
readPeopleArray(jsonReader);
}
}
jsonReader.endObject();
for(Person p : people){
System.out.println(p.toString());
}
}
catch (NullPointerException e){
e.printStackTrace();
}
catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void readPeopleArray(JsonReader jsonReader) throws IOException {
jsonReader.beginArray();
while (jsonReader.hasNext()) {
readPersonObject(jsonReader);
}
jsonReader.endArray();
}
private void readPersonObject(JsonReader jsonReader) throws IOException {
String name = null;
String sirname = null;
Long social_no = null;
Long creadit_card_no = null;
jsonReader.beginObject();
while(jsonReader.hasNext()){
String key = jsonReader.nextName();
if(key.equals("details")){
jsonReader.beginObject();
while(jsonReader.hasNext()){
String detailKey = jsonReader.nextName();
if(detailKey.equals("social_no")){
social_no = jsonReader.nextLong();
}
else if(detailKey.equals("creadit_card_no")){
creadit_card_no = jsonReader.nextLong();
}
else{
jsonReader.skipValue();
}
}
jsonReader.endObject();
}
else if(key.equals("name")){
name = jsonReader.nextString();
}
else if(key.equals("sirname")){
sirname = jsonReader.nextString();
}
}
jsonReader.endObject();
people.add(new Person(name, sirname, social_no, creadit_card_no));
}
}
答案 1 :(得分:1)
目前似乎杰克逊不支持开箱即用的功能,可以从嵌套路径中映射字段。
有一个open issue要求提供这样的功能,但这是一个什么时候完成的问题。
相反,通过使用<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="Of0UWpK5bEo" class="status">
<span class="caption">Text below the image</span>
</div>
<div id="1qYMJjTxJnM" class="status">
<span class="caption">Text below the image</span>
</div>
<div id="8aAab7gxbEg" class="status">
<span class="caption">Text below the image</span>
</div>
注释,可以将嵌套对象序列化为json中的第一级属性。
因此,为了克服这个问题,似乎唯一的方法是编写一个自定义反序列化器,您可以将其映射到您的类,并使用它来根据需要创建该类的实例。
答案 2 :(得分:1)
如果你有一个非常非常大的文件,我建议使用Gson的自定义反序列化程序执行此操作,但我不会使用JsonDeserializer
接口;使用TypeAdapter
接口,因为它更高效(source)。我认为@codemonkey有一个非常好的答案,但它过于复杂,可以更简单地完成。具体来说,你永远不应该自己构建这些字符串(使用sb.append()
),你应该远离JsonDeserializer
。
首先,创建自定义TypeAdapter
public class PersonTypeAdapter extends TypeAdapter<Person> {
@Override
public void write(JsonWriter out, Person value) throws IOException {
if (value == null) {
out.nullValue();
return;
}
out.beginObject();
out.name("name").value(value.name);
out.name("sirname").value(value.sirname);
out.name("details");
out.beginObject();
out.name("social_no").value(value.social_no);
out.name("creadit_card_no").value(value.creadit_card_no);
out.endObject();
out.endObject();
}
@Override
public Person read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return null;
}
reader.beginObject();
validateName(reader, "name");
String name = reader.nextString();
validateName(reader, "sirname");
String sirname = reader.nextString();
validateName(reader, "details");
reader.beginObject();
validateName(reader, "social_no");
String social_no = reader.nextString();
validateName(reader, "creadit_card_no");
String creadit_card_no = reader.nextString();
reader.endObject();
reader.endObject();
return new Person(name, sirname, social_no, creadit_card_no);
}
private void validateName(JsonReader reader, String string) throws IOException {
String name = reader.nextName();
if(!string.equals(name)) {
throw new JsonSyntaxException("Expected: \"" + string + "\", got \"" + name + "\"");
}
}
}
而且,你的POJO,显然:
public class Person {
public final String name;
public final String sirname;
public final String social_no;
public final String creadit_card_no;
public Person(String name, String sirname, String social_no,
String creadit_card_no) {
this.name = name;
this.sirname = sirname;
this.social_no = social_no;
this.creadit_card_no = creadit_card_no;
}
@Override
public String toString() {
return String.format(
"Person [name=%s, sirname=%s, social_no=%s, creadit_card_no=%s]", name,
sirname, social_no, creadit_card_no);
}
}
然后,您可以使用此处的方法从文件中解析Json。 /test.json
只是您在问题中提供的示例。
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;
public class PersonExample {
public static void main(String... args) {
InputStreamReader streamReader = new InputStreamReader(
PersonExample.class.getResourceAsStream("/test.json"));
PeopleWrapper wrapper = parseJSON(streamReader);
System.out.println(wrapper.people);
}
public static class PeopleWrapper {
@SerializedName("people")
public List<Person> people;
}
public static PeopleWrapper parseJSON(Reader jsonInput) {
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Person.class, new PersonTypeAdapter());
Gson gson = builder.create();
PeopleWrapper peopleWrapper = gson.fromJson(jsonInput, PeopleWrapper.class);
return peopleWrapper;
}
}
该程序输出:
[Person [name=test1, sirname=test2, social_no=1234567, creadit_card_no=34582342309], Person [name=test3, sirname=test4, social_no=12345679, creadit_card_no=345823423090]]
因此,您的实际问题比您最初描述的问题复杂得多。我会告诉你一个你需要的TypeAdapter
的骨架,你可以弄清楚其余部分。基本上,在您完成后创建Plan
对象,然后为每个外部JSON键处理该值。
switch
声明中处理它。您应该假设JSON格式正确,如果不是,请让Gson
抛出异常。只要告诉它预计接下来会发生什么。
这里有一些代码可以向您展示这个想法:
import java.io.IOException;
import com.google.gson.*;
import com.google.gson.stream.*;
public class PlanTypeAdapter extends TypeAdapter<Plan> {
private final String TAG = PlanTypeAdapter.class.getSimpleName();
@Override
public void write(JsonWriter out, Plan value) throws IOException {
Log.d(TAG, "WRITE");
}
@Override
public Plan read(JsonReader reader) throws IOException {
Log.d(TAG, "READ");
Plan plan = new Plan();
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return null;
}
reader.setLenient(false);
reader.beginObject();
while (!(reader.peek() == JsonToken.END_OBJECT)) {
switch (reader.nextName()) {
case "national_plan":
handleNationalPlan(reader, plan);
break;
case "bill_total":
handleBillTotal(reader, plan);
break;
case "contract_end":
plan.setContract_end(reader.nextString());
break;
case "data_level_gb":
plan.setData_level_gb(reader.nextString());
break;
case "data_level_id":
plan.setData_level_id(reader.nextInt());
break;
case "days_to_end":
plan.setDays_to_switch(reader.nextInt());
break;
case "direct_from_operator":
plan.setDirect_from_operator(reader.nextBoolean());
break;
case "calculation_amount":
plan.setCalculationAmount(reader.nextDouble());
break;
case "network_generation_name":
plan.setNetwork_generation_(reader.nextString());
break;
case "partner_plan_id":
plan.setPartner_plan_id(reader.nextString());
break;
case "payment_level":
plan.setPayment_level(reader.nextString());
break;
case "payment_level_id":
plan.setPayment_level_id(reader.nextInt());
break;
case "roaming_amount":
plan.setRoaming_amount(reader.nextDouble());
break;
case "savings_amount":
plan.setSavings_amount(reader.nextDouble());
break;
case "savings_avg":
plan.setSavings_avg(reader.nextDouble());
break;
case "savings_percents":
plan.setSavings_percents(reader.nextInt());
break;
case "yearly_id":
case "handset":
case "internals":
case "consumer_id":
case "calculation_details":
case "operator":
case "total":
case "international_plan":
case "contract_length":
case "zone":
case "externals":
case "cancel_fee":
case "transformers":
case "one-offs":
case "flow":
case "roaming_plan":
case "_id":
// You can use this to ignore the keys you don't care about
default:
Log.d(TAG, "DEFAULT " + reader.peek() + "");
reader.skipValue();
break;
}
}
reader.endObject();
return plan;
}
private void handleNationalPlan(JsonReader reader, Plan plan) throws IOException {
reader.beginObject();
while (!(reader.peek() == JsonToken.END_OBJECT)) {
switch(reader.nextName()) {
case "contract_length":
break;
case "name":
break;
case "country":
// etc.
}
}
reader.endObject();
}
private void handleBillTotal(JsonReader reader, Plan plan) throws IOException {
}
// etc.
}