Java对Json数据解析

JSON数据解析

最近一直在做钱包中间件,对于区块链的数据进行解析,对Json数据的解析,加深了了解。

项目中使用Maven的依赖

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


         com.alibaba
         fastjson
         1.2.9

解析的Json例子如下:

{
    "actions": [
        {
            "global_action_seq": 22744511,
            "account_action_seq": 0,
            "block_num": 8690464,
            "block_time": "2018-09-05T07:31:36.500",
            "action_trace": {
                "receipt": {
                    "receiver": "jxseostest11",
                    "act_digest": "a65862407737e83a03d7bff8cda54dd429e1fefa59dd457fe339e2ec64b4ef65",
                    "global_sequence": 22744511,
                    "recv_sequence": 1,
                    "auth_sequence": [
                        [
                            "eosfaucet111",
                            4085895
                        ]
                    ],
                    "code_sequence": 2,
                    "abi_sequence": 2
                },
                "act": {
                    "account": "eosio.token",
                    "name": "transfer",
                    "authorization": [
                        {
                            "actor": "eosfaucet111",
                            "permission": "active"
                        }
                    ],
                    "data": {
                        "from": "eosfaucet111",
                        "to": "jxseostest11",
                        "quantity": "100.0000 EOS",
                        "memo": ""
                    },
                    "hex_data": "1042c80a69b330551042c62a63aa707f40420f000000000004454f530000000000"
                },
                "elapsed": 24,
                "cpu_usage": 0,
                "console": "",
                "total_cpu_usage": 0,
                "trx_id": "92b74efb82a38cf2991b68e17baf113ae3ad202298936094aa952e2f4d7c9553",
                "inline_traces": []
            }
        }
}

一般情况下,接受数据的是String类型,也就是可以刚开始拿到这些数据是String类型,进行一步步的解析。

 //String数据转换为Json(如果是Json数据无需进行这行代码)
 JSONObject returnJson = JSONObject.parseObject(return_str);
 //获取actions的一组数据
 JSONArray actionArray = returnJson.getJSONArray("actions");
 //对actions的一组数据进行解析
 for(int index = 0;index < actionArray.size();index++){
       //对每一个action进行解析
       JSONObject actionInfo = actionArray.getJSONObject(index);
       //获取数据
       String blockNum = actionInfo.getString("block_num");
       String blockTime = actionInfo.getString("block_time");
       String trxId = actionInfo.getString("trx_id");
       String actionTrace = actionInfo.getString("action_trace");
       //获取action_trace节点的数据
       JSONObject actionTraceJson = JSONObject.parseObject(actionTrace);
       //获取action_trace节点下的act的Json格式数据
       String act = actionTraceJson.getString("act");
       JSONObject actJson = JSONObject.parseObject(act);
       //获取act下面的Array
       JSONArray authorizationArray = actJson.getJSONArray("authorization");
       //获取authorizationArray下面的数据
       for(int aIndex = 1;aIndex < authorizationArray.size();aIndex++){
        	//对每一个authorization进行解析
        	JSONObject authorizationInfo = authorizationArray.getJSONObject(aIndex);
        	String actor = authorizationInfo.getString("actor");
            String permission = authorizationInfo.getString("permission");
        }
       //获取act的Json格式数据
        String account = actJson.getString("account");
        String name = actJson.getString("name");
        String data = actJson.getString("data");
        //获取data节点下面的数据
        JSONObject data_json = JSONObject.parseObject(data);
        String from = data_json.getString("from");
        String to = data_json.getString("to");
        String quantity = data_json.getString("quantity");
        String memo = data_json.getString("memo");
   }

 

你可能感兴趣的:(杂七杂八)