java 对Json数据进行处理包括json解析以及json的简单用法 使用fastjson

SON 语法


JSON 语法是 JavaScript 语法的子集。


JSON 语法规则

JSON 语法是 JavaScript 对象表示语法的子集。

  • 数据在名称/值对中
  • 数据由逗号分隔
  • 大括号 {} 保存对象
  • 中括号 [] 保存数组,数组可以包含多个对象
  • JSON 名称/值对

    JSON 数据的书写格式是:。

    key : value

    名称/值对包括字段名称(在双引号中),后面写一个冒号,然后是值:

  • JSON 值

    JSON 值可以是:

  • 数字(整数或浮点数)
  • 字符串(在双引号中)
  • 逻辑值(true 或 false)
  • 数组(在中括号中)
  • 对象(在大括号中)
  • null
  • SON工具包

      A. 四大JSON类库:Google公司的Gson、阿里巴巴的FastJson、Jackson、Json-lib;

{
    "errorCode":"0",
    "Result":{
        "orientation":"",
        "regions":[
            {
                "boundingBox":"13,15,584,15,584,83,13,83",
                "dir":"h",
                "lang":"",
                "lines":[
                    [
                        {
                            "boundingBox":"13,22,110,22,110,76,13,76",
                            "text_height":53,
                            "words":[
                                {
                                    "boundingBox":"13,23,74,22,74,75,13,76",
                                    "word":"13."
                                },
                                {
                                    "boundingBox":"86,22,110,22,110,75,86,75",
                                    "word":"若"
                                }
                            ],
                            "text":"13.若",
                            "type":"text"
                        },
                        {
                            "boundingBox":"111,15,326,15,326,82,111,82",
                            "text_height":67,
                            "words":[
                                {
                                    "boundingBox":"111,15,326,15,326,82,111,82",
                                    "word":" x = ( \sqrt { 3 } { - 5 } ) ^ { 3 }"
                                }
                            ],
                            "text":"x = ( \sqrt { 3 } { - 5 } ) ^ { 3 }",
                            "type":"formula"
                        },
                        {
                            "boundingBox":"327,22,384,22,384,76,327,76",
                            "text_height":53,
                            "words":[
                                {
                                    "boundingBox":"327,22,338,22,338,75,327,75",
                                    "word":","
                                },
                                {
                                    "boundingBox":"350,23,384,23,384,76,350,76",
                                    "word":"则"
                                }
                            ],
                            "text":",则",
                            "type":"text"
                        },
                        {
                            "boundingBox":"385,15,584,15,584,83,385,83",
                            "text_height":67,
                            "words":[
                                {
                                    "boundingBox":"385,16,584,15,584,82,385,83",
                                    "word":" \sqrt { - x - 1 } ="
                                }
                            ],
                            "text":"\sqrt { - x - 1 } =",
                            "type":"formula"
                        }
                    ]
                ]
            }
        ],
        "exif":"UP"
    }
}

可以看到这是一个比较复杂的json格式

 对于这种复杂的json需要层层解析后获取结果

package baseoflearn.dec;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.sql.SQLOutput;

/**
 * @author :zhaofuh
 * @date :Created in 2020/9/23 10:24
 * @description:用来把获取的json进行转换为可用的json
 * @modified By:
 * @version: 0.1$
 */
public class JsonTrans {
    public static void main(String[] args) {
        //从文件读取json
        File file=new File("C:\\Users\\admin\\Desktop\\结果.json");
        String content;

        {
            try {
                content = FileUtils.readFileToString(file,"UTF-8");
                JSONObject jsonObject = new JSONObject();
                jsonObject = JSONObject.parseObject(content);
              //Result层
                String Result = jsonObject.getString("Result");

                //regions层
                jsonObject = JSONObject.parseObject(Result);
                JSONArray regions = jsonObject.getJSONArray("regions");
               // System.out.println(regions);
                String [][] Data ;
                String alltext ="";
                for (int j = 0; j < regions.size(); j++) {
                    String boundingBox = regions.getJSONObject(j).getString("boundingBox");
                    JSONArray lines = regions.getJSONObject(j).getJSONArray("lines");
                    JSONArray lines1=null;
                    Data = new String[lines.size()][];
                    for (int k = 0; k < lines.size(); k++) {
                       lines1 = lines.getJSONArray(k);

                        Data[k] = new String[lines1.size()];
                        for (int o = 0; o <lines1.size() ; o++) {
                            Data[k][o] = lines1.getString(o);
                            jsonObject = JSONObject.parseObject(lines1.getString(o));

                        String text = jsonObject.getString("text");
                        alltext=alltext+text;
                            System.out.println(text);
                            //JSONArray text = jsonObject.getJSONArray("text");
                        }
                       }
                }
                int dot = alltext.indexOf(".");
                String result = alltext.substring(0,dot+1);
                //String
                System.out.println(result);
                //System.out.println(alltext);
//
//                    }
//                }
//                String lines = jsonObject.getString("lines");
//                System.out.println(lines);

//                for (int i = 0; i < Result.size(); i++) {
//                    JSONArray regions = Result.getJSONObject(i).getJSONArray("regions");
//                    for (int j = 0; j < regions.size(); j++) {
//                        String boundingBox = regions.getJSONObject(j).getString("boundingBox");
//                        JSONArray lines = regions.getJSONObject(j).getJSONArray("lines");
//
//                        for (int k = 0; k < lines.size(); k++) {
//                            String text = lines.getJSONObject(k).getString("text");
//                            System.out.println(text);
//                        }
//                    }
//                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

                                                                                                                                                                                                                    解析的过程上面的代码写的很详细可以自己查看并测试                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值