JackJson解析json的字符串格式

从前台获取了一个xml,然后解析xml的属性的时候,其属性值是一个json的String类型.所以在解析json的时候,给出的问题是:

1,不管什么对象类型,根据json的构造,根据的key值,去取对应的value值;

2,将操作的json的对象,定义为对应的java对象,然后将json转换为对应的java对象.取java对象的属性值,即是处理的结果值.

 

两种不同的思路,性能和可维护性上面,个人感觉还是后者相对方便一点..贴下代码,看看...

 

第一种方式::

其json的直观视觉是:

package azkabanproject;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

public class ParseJsonString {

    public static void main(String[] args) {

        String jsonString = "{\n" +
                "          \"sources\":[\n" +
                "                {\n" +
                "                    \"baseList\":{\n" +
                "                        \"tableName\":\"trs_action_dzwl_zm1\",\n" +
                "                        \"tableAlias\":\"50\",\n" +
                "                        \"tableSize\":\"10000\",\n" +
                "                        \"tableFields\":[\n" +
                "                            {\n" +
                "                                \"field\":\"name1\",\n" +
                "                                \"fieldDesc\":\"名称1\",\n" +
                "                                \"fieldInputtype\":\"0\"\n" +
                "                            },\n" +
                "                            {\n" +
                "                                \"field\":\"name2\",\n" +
                "                                \"fieldDesc\":\"名称2\",\n" +
                "                                \"fieldInputtype\":\"1\"\n" +
                "                            }\n" +
                "                        ]\n" +
                "                    },\n" +
                "                    \"infoList\":[\n" +
                "                        {\n" +
                "                            \"field\":\"start_time\",\n" +
                "                            \"fieldContent\":\"20170724000000到20170724101010\",\n" +
                "                            \"fieldDesc\":\"创建时间\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"field\":\"site_address\",\n" +
                "                            \"fieldContent\":\"南苑大厦\",\n" +
                "                            \"fieldDesc\":\"详细地址\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"field\":\"name\",\n" +
                "                            \"fieldContent\":\"自定义名称1\",\n" +
                "                            \"fieldDesc\":\"名称\"\n" +
                "                        }\n" +
                "                    ]\n" +
                "                },\n" +
                "                {\n" +
                "                    \"baseList\":{\n" +
                "                        \"tableName\":\"trs_action_dzwl_zm1\",\n" +
                "                        \"tableAlias\":\"50\",\n" +
                "                        \"tableSize\":\"10000\",\n" +
                "                        \"tableFields\":[\n" +
                "                            {\n" +
                "                                \"field\":\"name1\",\n" +
                "                                \"fieldDesc\":\"名称1\",\n" +
                "                                \"fieldInputtype\":\"0\"\n" +
                "                            },\n" +
                "                            {\n" +
                "                                \"field\":\"name2\",\n" +
                "                                \"fieldDesc\":\"名称2\",\n" +
                "                                \"fieldInputtype\":\"1\"\n" +
                "                            }\n" +
                "                        ]\n" +
                "                    },\n" +
                "                    \"infoList\":[\n" +
                "                        {\n" +
                "                            \"field\":\"start_time\",\n" +
                "                            \"fieldContent\":\"20170724000000到20170724101010\",\n" +
                "                            \"fieldDesc\":\"创建时间\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"field\":\"site_address\",\n" +
                "                            \"fieldContent\":\"南苑大厦\",\n" +
                "                            \"fieldDesc\":\"详细地址\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"field\":\"name\",\n" +
                "                            \"fieldContent\":\"自定义名称1\",\n" +
                "                            \"fieldDesc\":\"名称\"\n" +
                "                        }\n" +
                "                    ]\n" +
                "                }\n" +
                "            ],\n" +
                "        \"outFields\":[\n" +
                "                {\n" +
                "                    \"field\":\"name1\",\n" +
                "                    \"fieldDesc\":\"名称1\",\n" +
                "                    \"fieldInputtype\":\"0\"\n" +
                "                },\n" +
                "                {\n" +
                "                    \"field\":\"name2\",\n" +
                "                    \"fieldDesc\":\"名称2\",\n" +
                "                    \"fieldInputtype\":\"1\"\n" +
                "                }\n" +
                "             ]\n" +
                " }";

        parse(jsonString);
    }
    public static void parse(String jsonString) {
      //  String jsonString = "";
        Gson json = new Gson();
        JsonObject jsb = json.fromJson(jsonString, JsonObject.class);
        JsonArray sources = jsb.getAsJsonArray("sources");  //数组
        for (JsonElement ja : sources) {
            JsonElement jba = ja.getAsJsonObject().get("baseList");
            String tableName = jba.getAsJsonObject().get("tableName").getAsString();
            String tableAlias = jba.getAsJsonObject().get("tableAlias").getAsString();
            String tableSize = jba.getAsJsonObject().get("tableSize").getAsString();
            System.out.println(tableName+"___"+tableAlias+"___"+tableSize);

            JsonArray tableFields = jba.getAsJsonObject().get("tableFields").getAsJsonArray();
            for (JsonElement tableField : tableFields) {
                String field = tableField.getAsJsonObject().get("field").getAsString();
                String fieldDesc = tableField.getAsJsonObject().get("fieldDesc").getAsString();
                String fieldInputtype = tableField.getAsJsonObject().get("fieldInputtype").getAsString();
                System.out.println(field+"___"+fieldDesc+"___"+fieldInputtype);
            }

            JsonArray infoList = ja.getAsJsonObject().get("infoList").getAsJsonArray();
            for (JsonElement info : infoList) {
                String field = info.getAsJsonObject().get("field").getAsString();
                String fieldContent = info.getAsJsonObject().get("fieldContent").getAsString();
                String fieldDesc = info.getAsJsonObject().get("fieldDesc").getAsString();
                System.out.println(field+"___"+fieldContent+"___"+fieldDesc);
            }

        }

        JsonArray outFields = jsb.getAsJsonArray("outFields");
        for (JsonElement outField : outFields) {

           String field= outField.getAsJsonObject().get("field").getAsString();
           String fieldDesc=  outField.getAsJsonObject().get("fieldDesc").getAsString();
           String fieldInputtype=outField.getAsJsonObject().get("fieldInputtype").getAsString();
            System.out.println(field+"___"+fieldDesc+"___"+fieldInputtype);
        }
    }
}

以上是第一种方式,使用的是Gson,然后根据json结构直接操作json数据类型.

 

第二种是:构建对象

对象JsonValue

package com.trs.idap.web.rest.util;

import java.io.Serializable;
import java.util.Arrays;

/**
 * Created by Administrator on 2018/7/25.
 * 描述:
 * @author Young
 * @create 2018-07-25 17:44
 */
public class JsonValue implements Serializable{
    private BaseInfo[] sources;
    private OutFields[] outFields;

    @Override
    public String toString() {
        return "JsonValue{" +
            "sources=" + Arrays.toString(sources) +
            ", outFields=" + Arrays.toString(outFields) +
            '}';
    }

    public OutFields[] getOutFields() {
        return outFields;
    }

    public void setOutFields(OutFields[] outFields) {
        this.outFields = outFields;
    }

    public BaseInfo[] getSources() {
        return sources;
    }

    public void setSources(BaseInfo[] sources) {
        this.sources = sources;
    }

}

对象BaseInfo

package com.trs.idap.web.rest.util;

import java.io.Serializable;
import java.util.Arrays;

/**
 * Created by Administrator on 2018/7/25.
 * 描述:
 *
 * @author Young
 * @create 2018-07-25 17:47
 */
public class BaseInfo implements Serializable{
    private BaseList baseList;
    private InfoList[] infoList;
    
    //TODO getter/setter/toString
}

对象BaseList

package com.trs.idap.web.rest.util;

import java.io.Serializable;
import java.util.Arrays;

/**
 * Created by Administrator on 2018/7/25.
 * 描述:
 *
 * @author Young
 * @create 2018-07-25 17:59
 */
public class BaseList implements Serializable{
    private String tableName;
    private String tableNameDesc;
    private String tableAlias;
    private String tableSize;
    private TableFields[]  tableFields;

   //TODO setter getter toString
}

对象 TableFields

package com.trs.idap.web.rest.util;

import java.io.Serializable;

/**
 * Created by Administrator on 2018/7/25.
 * 描述:
 *
 * @author Young
 * @create 2018-07-25 18:02
 */
public class TableFields implements Serializable{
    private String field;
    private String fieldDesc;
    private String fieldInputtype;

 //TODO setter getter toString
}

对象InfoList

package com.trs.idap.web.rest.util;

import java.io.Serializable;

/**
 * Created by Administrator on 2018/7/25.
 * 描述:
 *
 * @author Young
 * @create 2018-07-25 18:04
 */
public class InfoList implements Serializable {
    private String field;
    private String fieldContent;
    private String fieldDesc;

   //TODO getter setter toString
}

 

对象OutFields

package com.trs.idap.web.rest.util;

import java.io.Serializable;

/**
 * Created by Administrator on 2018/7/25.
 * 描述:
 *
 * @author Young
 * @create 2018-07-25 17:55
 */
public class OutFields implements Serializable {
    private String field;
    private String fieldDesc;
    private String fieldInputtype;

  //TODO getter setter toString
}

由上图可见.....该json总共建立了6个java对象.但是对于后期的维护性还是比较方便的....

使用方法解析时:

package com.trs.idap.web.rest.util;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

/**
 * Created by Administrator on 2018/7/25.
 * 描述:解析json格式
 *
 * @author Young
 * @create 2018-07-25 17:07
 */
public class JsonUtils {
    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        JsonValue jsonValue = mapper.readValue(jsonString, JsonValue.class);

        OutFields[] outFields = jsonValue.getOutFields();
        //字段值,字段   ===>columnList columnListComment
        StringBuffer sbFieldList = new StringBuffer();
        StringBuffer sbFieldListComment = new StringBuffer();
        Boolean flag = false;
        for (OutFields outField : outFields) {
            System.err.println(outField.getField());
            System.out.println(outField.getFieldDesc());
            System.out.println(outField.getFieldInputtype());
            if (!flag) {
                sbFieldList.append(outField.getField());
                sbFieldListComment.append(outField.getFieldDesc());
                flag = true;
            } else {
                sbFieldList.append(",").append(outField.getField());
                sbFieldListComment.append(",").append(outField.getFieldDesc());
            }
        }
        System.out.println("字段列为:======>" + sbFieldList.toString() + ",   字段名为===:" + sbFieldListComment);
        BaseInfo[] sources = jsonValue.getSources();
        for (BaseInfo baseInfo : sources) {
            BaseList baseList = baseInfo.getBaseList();
            System.out.println(baseList.getTableAlias());
            System.out.println(baseList.getTableFields());
            System.out.println(baseList.getTableName());
            System.out.println(baseList.getTableNameDesc());
            System.out.println(baseList.getTableSize());
            InfoList[] infoList = baseInfo.getInfoList();
            for (InfoList info : infoList) {
                System.out.println(info.getField());
                System.out.println(info.getFieldContent());
                System.out.println(info.getFieldDesc());
            }
        }
    }

  

}

真正使用的就是获取到指定的类型

        ObjectMapper mapper = new ObjectMapper();
        JsonValue jsonValue = mapper.readValue(jsonString, JsonValue.class);

构建起来还很方便的..只要将对象关系处理好.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值