栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JSON对象遍历并按照原顺序输出

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JSON对象遍历并按照原顺序输出

@TOCJSON对象遍历并按照原顺序输出

需求

在工作中,往往遇会遇到对JSON对象的处理,在这里记录一下我需要遍历JSON对象,并对JSON中的每一个加密字段进行解密,并将解密后的JSON元素按照原顺序进行输出。

Java代码
package test;

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


import java.io.*;
import java.util.Map;


public class JsonLoop {

    
    public static JSONObject getObject() {
        char cbuf[] = new char[10000];
        InputStreamReader input = null;

        try {
            input = new InputStreamReader(new FileInputStream(new File("src/main/java/test/test.json")), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        int len = 0;

        try {
            len = input.read(cbuf);
        } catch (IOException e) {
            e.printStackTrace();
        }

        String text = new String(cbuf, 0, len);
        //1.构造一个json对象,在这里确保JSON对象中元素的顺序和JSONString中的数据不变
        JSONObject obj = JSONObject.parseObject(text, Feature.OrderedField);
        return obj;
    }

    
    public static JSONObject jsonLoop(Object object) {
        JSONObject newJson = new JSONObject(true);
        if (object instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) object;
            for (Map.Entry entry : jsonObject.entrySet()) {
                Object o = entry.getValue();
                String key = entry.getKey();
                if (o instanceof String) {
                    String vaule = (String) o;
                    newJson.put(key, vaule);
                } else if (o instanceof Integer) {
                    int vaule = (int) o;
                    newJson.put(key, vaule);
                } else if (o instanceof Boolean) {
                    boolean vaule = (boolean) o;
                    newJson.put(key, vaule);
                } else if (o instanceof Double) {
                    double vaule = (double) o;
                    newJson.put(key, vaule);
                } else if (o instanceof Float) {

                    float vaule = (float) o;
                    newJson.put(key, vaule);
                } else if (o instanceof Long) {
                    long vaule = (long) o;
                    newJson.put(key, vaule);
                } else if (o instanceof JSONArray) {
                    JSONArray jsonArray = (JSONArray) o;
                    JSONArray newJArray = new JSONArray();
                    for (int i = 0; i < jsonArray.size(); i++) {
                        JSONObject sub = jsonLoop(jsonArray.get(i));
                        newJArray.add(sub);
                    }
                    newJson.put(key, newJArray);
                } else if (o instanceof JSONObject) {
                    JSONObject sub = jsonLoop(o);
                    newJson.put(key, sub);
                } else {
                    newJson.put(key, o);
                }
            }
        }

        return newJson;
    }

    public static void main(String[] args) {
        JSONObject jsonObject = getObject();
        System.out.println(jsonObject.toJSONString());
        System.out.println();
        JSONObject newJson = jsonLoop(jsonObject);
        System.out.println(newJson.toJSONString());
    }
}
测试数据
{
  "test1": "wx9fdb8ble7ce3c68f",
  "test2": "123456789",
  "testData1": {
    "testdatason1": "97895455",
    "testdatason2": 3,
    "testData2": [
      {
        "testshuzu1": "12",
        "testshuzu1": "11"
      },
      {
        "testshuzu2": "13",
        "testshuzu2": "14"
      },
      {
        "testshuzu3": "15",
        "testshuzu3": "16"
      }
    ]
  }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/839150.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号