添加依赖:
// 使用 argo解析 json,源码请查看 https://svn.code.sf.net/p/argo/code/tags/5.5
implementation 'net.sourceforge.argo:argo:5.5'
以下是封装的一个工具类:
import java.util.ArrayList;
import java.util.List;
import argo.format.PrettyJsonFormatter;
import argo.jdom.JdomParser;
import argo.jdom.JsonNode;
import argo.jdom.JsonNodeBuilders;
import argo.jdom.JsonNodeFactories;
import argo.jdom.JsonObjectNodeBuilder;
import argo.jdom.JsonStringNode;
import argo.saj.InvalidSyntaxException;
public class ArgoUtil {
private static PrettyJsonFormatter JSON_FORMATTER = new PrettyJsonFormatter();
private static JdomParser JDOM_PARSER = new JdomParser();
public static String generateJson() {
// JsonNode是不可变的
JsonNode json = JsonNodeFactories.object(
JsonNodeFactories.field(
"name",
JsonNodeFactories.string("Black Lace")
),
JsonNodeFactories.field(
"sales",
JsonNodeFactories.number("110921")
),
JsonNodeFactories.field(
"totalRoyalties",
JsonNodeFactories.number("10223.82")
),
JsonNodeFactories.field(
"singles",
JsonNodeFactories.array(
JsonNodeFactories.string("Superman"),
JsonNodeFactories.string("Agadoo")
)
)
);
String jsonText = JSON_FORMATTER.format(json);
return jsonText;
}
public static String generateJson(Datas datas) {
// JsonNode是不可变的
List<String> stringList = datas.getSingles();
JsonStringNode jsonStringNode = null;
JsonNode[] jsonNodes = {};
for (int i = 0; i < stringList.size(); i++) {
jsonNodes[i] = JsonNodeFactories.string(stringList.get(i));
}
JsonNode json = JsonNodeFactories.object(
JsonNodeFactories.field(
"name",
JsonNodeFactories.string(datas.getName())
),
JsonNodeFactories.field(
"sales",
JsonNodeFactories.number(datas.getSales())
),
JsonNodeFactories.field(
"totalRoyalties",
JsonNodeFactories.number(datas.getTotalRoyalties())
),
JsonNodeFactories.field(
"singles",
JsonNodeFactories.array(jsonNodes)
)
);
String jsonText = JSON_FORMATTER.format(json);
return jsonText;
}
public static String generateJson2() {
// JsonNode可以使用JsonNodeBuilders类中的方法以可变的方式构建相同的方法:
JsonObjectNodeBuilder builder = JsonNodeBuilders.anObjectBuilder()
.withField("name", JsonNodeBuilders.aStringBuilder("Black Lace"))
.withField("sales", JsonNodeBuilders.aNumberBuilder("110921"))
.withField("totalRoyalties", JsonNodeBuilders.aNumberBuilder("10223.82"))
.withField("singles", JsonNodeBuilders.anArrayBuilder()
.withElement(JsonNodeBuilders.aStringBuilder("Superman"))
.withElement(JsonNodeBuilders.aStringBuilder("Agadoo"))
);
JsonNode json = builder.build();
String jsonText = JSON_FORMATTER.format(json);
return jsonText;
}
public static String generateJson2(Datas datas) {
// JsonNode可以使用JsonNodeBuilders类中的方法以可变的方式构建相同的方法:
JsonObjectNodeBuilder builder = JsonNodeBuilders.anObjectBuilder()
.withField("name", JsonNodeBuilders.aStringBuilder(datas.getName()))
.withField("sales", JsonNodeBuilders.aNumberBuilder(datas.getSales()+""))
.withField("totalRoyalties", JsonNodeBuilders.aNumberBuilder(datas.getTotalRoyalties()+""))
.withField("singles", JsonNodeBuilders.anArrayBuilder()
.withElement(JsonNodeBuilders.aStringBuilder(datas.getSingles().get(0)))
.withElement(JsonNodeBuilders.aStringBuilder(datas.getSingles().get(1)))
);
JsonNode json = builder.build();
String jsonText = JSON_FORMATTER.format(json);
return jsonText;
}
// {
// "name": "Black Lace",
// "sales": 110921,
// "totalRoyalties": 10223.82,
// "singles": [
// "Superman", "Agadoo"
// ]
// }
public static Datas json2Object(String jsonText) {
try {
JsonNode json = JDOM_PARSER.parse(jsonText);
Datas datas = new Datas();
datas.setName("Black Lace");
datas.setSales(110921);
datas.setTotalRoyalties((long) 10223.82);
ArrayList<String> list = new ArrayList<>();
list.add("Superman");
list.add("Agadoo");
datas.setSingles(list);
return datas;
} catch (InvalidSyntaxException e) {
e.printStackTrace();
}
return null;
}
}
public class Datas {
private String name;
private int sales;
private long totalRoyalties;
private List<String> singles;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSales() {
return sales;
}
public void setSales(int sales) {
this.sales = sales;
}
public long getTotalRoyalties() {
return totalRoyalties;
}
public void setTotalRoyalties(long totalRoyalties) {
this.totalRoyalties = totalRoyalties;
}
public List<String> getSingles() {
return singles;
}
public void setSingles(List<String> singles) {
this.singles = singles;
}
@Override
public String toString() {
return "Datas{" +
"name='" + name + '\'' +
", sales=" + sales +
", totalRoyalties=" + totalRoyalties +
", singles=" + singles +
'}';
}
}