Android docking with .net (C#) server (1): parsing DataTable, DataSet type and pagination Json data

〇 Preface

There are DataTable and DataSet types of objects in the .net platform C#, and they are very convenient and commonly used. The server side of our current project is developed in C#. When returning response data to the front desk, it directly returns DataTable and DataSet type objects after Json serialization. There is no corresponding DataTable and DataSet objects under Android, so the first thing when the Android side receives the response data is to parse the Json data. For such a fixed format, it is a good practice to parse it at the bottom level and directly give it to The business layer returns available List data. So I encapsulated two tool classes for analyzing Json data of DataTable and DataSet respectively.

1. Parse the Json data of DataTable type

Examples of data of the DataTable type are as follows:

{"TableName":"dt","Table":[{"Row":"1","MACHINE_INSTANCE_ID":"66848","PLANT_ID":"5","PLANT_NAME":"总装1#生产车间","WS_ID":"214","MACHINE_PRODUCT_ID":"2935","SERIES_NAME":"精雕","MODEL_NAME":"CarverPMS23_A8","PRODUCT_CODE":"5593.0033.110014","MAC_POSITION":"A-2-1","PROCESS_BARCODE":"INL01312003418","MACHINE_STATE":"1","MAC_WS_ID":"68622","MAC_AREA_WIDTH":"2000","MAC_AREA_HEIGHT":"2000","PROGRESS_STATE":"59","TEST_CODE":"648","PRODUCT_STATE":"5"},{"Row":"2","MACHINE_INSTANCE_ID":"67280","PLANT_ID":"5","PLANT_NAME":"总装1#生产车间","WS_ID":"214","MACHINE_PRODUCT_ID":"641","SERIES_NAME":"精雕","MODEL_NAME":"JDLVG400E-A8","PRODUCT_CODE":"5593.0053.110032","MAC_POSITION":"A-2-3","PROCESS_BARCODE":"INL01312003474","MACHINE_STATE":"1","MAC_WS_ID":"68880","MAC_AREA_WIDTH":"2500","MAC_AREA_HEIGHT":"2000","PROGRESS_STATE":"55","TEST_CODE":"107","PRODUCT_STATE":"5"},{"Row":"3","MACHINE_INSTANCE_ID":"67279","PLANT_ID":"5","PLANT_NAME":"总装1#生产车间","WS_ID":"214","MACHINE_PRODUCT_ID":"641","SERIES_NAME":"精雕","MODEL_NAME":"JDLVG400E-A8","PRODUCT_CODE":"5593.0053.110032","MAC_POSITION":"A-2-4","PROCESS_BARCODE":"INL01312003473","MACHINE_STATE":"1","MAC_WS_ID":"68881","MAC_AREA_WIDTH":"2500","MAC_AREA_HEIGHT":"2000","PROGRESS_STATE":"55","TEST_CODE":"106","PRODUCT_STATE":"5"}]}

The data of the DataTable type is the encapsulation of the row set plus the table name, which corresponds to the Json analysis, as follows:

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

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * Description: 用于Json格式字符串数据传输的DataTable类型。
 * Copyright  : Copyright (c) 2020
 * Author     : mliuxb
 * Date       : 2021-03-11 09:00
 */
public class DataTable<T> {

    public String TableName;    //表名
    public ArrayList<T> Table;  //行集合

    /**
     * 构造函数
     * @param tableName
     *         表名
     */
    public DataTable(String tableName) {
        TableName = tableName;
        Table = new ArrayList<>();
    }

    @NonNull
    private static <T> ParameterizedType getType(@NonNull final Class<T> cls) {
        return new ParameterizedType() {
            @NonNull
            @Override
            public Type[] getActualTypeArguments() {
                return new Type[]{cls};
            }

            @NonNull
            @Override
            public Type getRawType() {
                return DataTable.class;
            }

            @Override
            public Type getOwnerType() {
                return null;
            }
        };
    }

    /**
     * 将json字符串解析为DataTable对象
     */
    @Nullable
    public static <T> DataTable<T> fromJson(@Nullable String json, @NonNull Class<T> cls) {
        //json为null、""(空字符串)、" "(空格或tab)时Gson解析都会返回null(不报异常),所以此处可不进行判空。
        try {
            if (json == null || json.isEmpty()) {
                //判断字符串是否合法
                return null;
            }
            Gson gson = new Gson();
            Type type = getType(cls);
            DataTable<T> dataTable = gson.fromJson(json, type);
            if (json.contains("NoneRow") && dataTable != null && dataTable.Table != null) {
                //判断是否存在空行标识。
                dataTable.Table.clear();
            }
            return dataTable;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 将json字符串解析为ArrayList
     */
    @NonNull
    public static <T> ArrayList<T> fromJsonToList(@Nullable String json, @NonNull Class<T> cls) {
        //json为null、""(空字符串)、" "(空格或tab)时Gson解析都会返回null(不报异常),所以此处可不进行判空。
        try {
            if (json == null || json.isEmpty() || json.contains("NoneRow")) {
                //判断字符串是否合法以及是否存在空行标识。
                return new ArrayList<>();
            }
            Gson gson = new Gson();
            Type type = getType(cls);
            DataTable<T> dataTable = gson.fromJson(json, type);
            if (dataTable == null || dataTable.Table == null) {
                return new ArrayList<>();
            } else {
                return dataTable.Table;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

    /**
     * 将JsonElement解析为DataTable对象
     */
    @Nullable
    static <T> DataTable<T> fromJson(@Nullable JsonElement element, @NonNull Class<T> cls) {
        try {
            if (element == null || element.isJsonNull()) {
                //判断字符串是否合法
                return null;
            }
            Gson gson = new Gson();
            Type type = getType(cls);
            DataTable<T> dataTable = gson.fromJson(element, type);
            if (element.toString().contains("NoneRow") && dataTable != null && dataTable.Table != null) {
                //判断是否存在空行标识。
                dataTable.Table.clear();
            }
            return dataTable;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 将JsonElement解析为ArrayList
     */
    @NonNull
    static <T> ArrayList<T> fromJsonToList(@Nullable JsonElement element, @NonNull Class<T> cls) {
        try {
            if (element == null || element.isJsonNull() || element.toString().contains("NoneRow")) {
                //判断字符串是否合法以及是否存在空行标识。
                return new ArrayList<>();
            }
            Gson gson = new Gson();
            Type type = getType(cls);
            DataTable<T> dataTable = gson.fromJson(element, type);
            if (dataTable == null || dataTable.Table == null) {
                return new ArrayList<>();
            } else {
                return dataTable.Table;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }
}

Second, parse the Json data of the DataSet type

Examples of Json data of DataSet type are as follows:

[{"TableName":"dt","Table":[{"FIRST_PHONE":"17333672366","USER_ID":"1674","USER_NAME":"姓名","PRODUCE_GROUP_ID":"1","PRODUCE_GROUP_NAME":"生产管理组"}]},{"TableName":"dt1","Table":[{"NUM":"1","BASIC_PROCESS_ID":"75","MACHINE_INSTANCE_ID":"21766","ACTUAL_HOUR":"0.140400","STANDARD_HOUR":"35.40","START_TIME":"2019-03-11 18:39","END_TIME":"2019-03-11 18:39","BP_NAME":"检验员用球杆仪检测","TEST_CODE":"L20170915","MODEL_NAME":"JDPMS16E_A8(16169)","POSITION":"清理车间:598","IS_SCHEDU":"否"},{"NUM":"2","BASIC_PROCESS_ID":"1196","MACHINE_INSTANCE_ID":"66936","ACTUAL_HOUR":"0.000000","STANDARD_HOUR":"3.60","START_TIME":"2020-11-21 09:29","END_TIME":"","BP_NAME":"组装联轴节","TEST_CODE":"100","MODEL_NAME":"JDWGM800_A10","POSITION":"移位入库至机械主机库仓位","IS_SCHEDU":"否"}]}]

Data of DataSet type is a collection of multiple DataTable types, and the corresponding Json data is parsed as follows:

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

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * Description: 用于Json格式字符串数据传输的DataSet类型。
 * Copyright  : Copyright (c) 2020
 * Author     : mliuxb
 * Date       : 2021-03-11 09:16
 */
public class DataSet {
    //private static final String TAG = "DataSet";

    //表集合
    //private ArrayList<DataTable<?>> tables = new ArrayList<>();
    private final JsonArray mJsonSet;

    /**
     * 构造函数
     * @param dataSetJson DataSet格式的Json字符串
     */
    public DataSet(@NonNull String dataSetJson) {
        final JsonElement setElement = new JsonParser().parse(dataSetJson);
        if (setElement != null && setElement.isJsonArray()) {
            mJsonSet = setElement.getAsJsonArray();
        } else {
            mJsonSet = null;
        }
    }

    @NonNull
    public <T> ArrayList<T> fromJsonToList(@Nullable String tableName, @NonNull Class<T> cls) {
        if (mJsonSet == null || tableName == null) {
            return new ArrayList<>();
        }
        for (final JsonElement tableElement : mJsonSet) {
            final JsonObject jsonTable = tableElement.getAsJsonObject();
            String strTableName = jsonTable.getAsJsonPrimitive("TableName").getAsString();
            if (tableName.equals(strTableName)) {
                return DataTable.fromJsonToList(jsonTable, cls);
            }
        }
        //无对应的tableName时,返回空集合
        return new ArrayList<>();
    }
}

Three, paginated Json data

The paginated Json data wraps the DataTable with another layer. An example is as follows:

The corresponding Json data analysis is as follows:

import com.google.gson.Gson;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import jdlf_scgl_zp_android.s005_service.wcf.DataTable;

/**
 * Description: PageDataSource
 * Copyright  : Copyright (c) 2020
 * Author     : mliuxb
 * Date       : 2021/03/11 10:47
 */
public class PageDataSource {

    private int TotalCount;
    private int pageCount;
    private String Table;

    @NonNull
    public static <T> ArrayList<T> fromJson(@Nullable String strJson, Class<T> classType) {
        try {
            Gson gsn = new Gson();
            //strJson为null、""(空字符串)、" "(空格或tab)时Gson解析都会返回null(不报异常),所以此处可不进行判空。
            PageDataSource pds = gsn.fromJson(strJson, PageDataSource.class);
            if (pds == null) {
                return new ArrayList<>();
            } else {
                return DataTable.fromJsonToList(pds.Table, classType);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }
}

 

Guess you like

Origin blog.csdn.net/beita08/article/details/114651414