前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Argo库解析sjon

Argo库解析sjon

作者头像
AWeiLoveAndroid
发布2019-07-25 16:54:15
5390
发布2019-07-25 16:54:15
举报
文章被收录于专栏:Flutter入门到实战

添加依赖:

代码语言:javascript
复制
  // 使用 argo解析 json,源码请查看 https://svn.code.sf.net/p/argo/code/tags/5.5
    implementation 'net.sourceforge.argo:argo:5.5'

以下是封装的一个工具类:

代码语言:javascript
复制
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;
    }

}
代码语言:javascript
复制
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 +
                '}';
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.07.24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档