fastjson解析文本

Posted 南=子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了fastjson解析文本相关的知识,希望对你有一定的参考价值。

文本中的json格式

20170215 18:00:00.001-{"visitTime":"1487152800001","ip":"xx.xxx.xxx.xxx","params":"{\"encode\":\"utf-8\",\"xxx\":\"0.0\",\"xxxx\":\"6869679\",\"resType\":\"json\",\"ak\":\"xxxxxxxxxxxxxxxxxxxxxxx\",\"xxx\":\"98\",\"uri\":\"/xx/xxx\",\"out_coord_type\":\"xxx\",\"coord_type\":\"xxxx\",\"domain\":null,\"location\":\"xxxx,xxxxx\",\"enc\":\"utf-8\",\"pois\":\"1\"}","device":"xxxxxx","sid":"xxxx"}

代码(用的是阿里的fastjson包)

  1 package com.entity;
  2 import java.io.BufferedReader;
  3 import java.io.BufferedWriter;
  4 import java.io.File;
  5 import java.io.FileNotFoundException;
  6 import java.io.FileOutputStream;
  7 import java.io.FileReader;
  8 import java.io.FileWriter;
  9 import java.io.IOException;
 10 import java.io.PrintStream;
 11 import java.math.BigDecimal;
 12 import java.text.DecimalFormat;
 13 
 14 import org.junit.Test;
 15 
 16 import com.alibaba.fastjson.JSON;
 17 import com.alibaba.fastjson.JSONObject;
 18 
 19 public class ReadAndWrite {
 20     public static void main(String[] args) {
 21         excute("F:\\xxxx.log");
 22     }
 23     public static void excute(String path){
 24         File file = new File(path);
 25         BufferedReader reader = null;
 26         String laststr = "";
 27         try {
 28          //System.out.println("以行为单位读取文件内容,一次读一整行:");
 29          reader = new BufferedReader(new FileReader(file));
 30          String tempString = null;
 31          int line = 1;
 32          //一次读入一行,直到读入null为文件结束
 33          while ((tempString = reader.readLine())!= null) {
 34               //显示行号
 35               //System.out.println("line " + line + ": " + tempString);
 36               JSONObject parseObject = JSON.parseObject(tempString);
 37               String string = parseObject.get("params").toString();
 38               JSONObject parseObject2 = JSON.parseObject(string);
 39 
 40               //活动车辆id
 41               String carID;
 42               if(parseObject2.get("vhcid")==null){
 43                   String ip=parseObject.get("ip").toString();
 44                   String carID1=MD5Utils.md5(ip).substring(0, 5);
 45                   int parseInt = Integer.parseInt(carID1, 16)+10000000;
 46                   carID=String.valueOf(parseInt);
 47               }else{
 48                   carID=parseObject2.get("vhcid").toString();
 49               }
 50               
 51               //获得定位时间
 52               String UTC1 = parseObject.get("visitTime").toString();
 53               long parseLong = Long.parseLong(UTC1);
 54               
 55               double parse=parseLong/1000;
 56               int parseInt=(int)parse;
 57               String UTC=String.valueOf(parseInt);
 58               //获得纬度和经度
 59               String du;
 60               if(parseObject2.get("location")!=null){
 61                   String du1=parseObject2.get("location").toString();
 62                   String[] split = du1.split(",");
 63                   BigDecimal bd = new BigDecimal(split[0]);
 64                   BigDecimal multiply = bd.multiply(new BigDecimal(100000));
 65                   DecimalFormat dcmFmt = new DecimalFormat("0.0");
 66                   //System.out.println(dcmFmt.format(multiply));
 67                   BigDecimal bd1 = new BigDecimal(split[1]);
 68                   BigDecimal multiply1 = bd1.multiply(new BigDecimal(100000));
 69                   DecimalFormat dcmFmt1 = new DecimalFormat("0.0");
 70                   //System.out.println(dcmFmt1.format(multiply1));
 71                  
 72                   du=dcmFmt.format(multiply).substring(0,dcmFmt.format(multiply).length()-2 )+","+dcmFmt1.format(multiply1).substring(0, dcmFmt1.format(multiply1).length()-2);
 73                   
 74               }else if(parseObject2.get("coords")!=null){
 75                   String du1=parseObject2.get("coords").toString();
 76                   String[] split = du1.split(",");
 77                   BigDecimal bd = new BigDecimal(split[0]);
 78                   BigDecimal multiply = bd.multiply(new BigDecimal(100000));
 79                   DecimalFormat dcmFmt = new DecimalFormat("0.0");
 80                   //System.out.println(dcmFmt.format(multiply));
 81                   BigDecimal bd1 = new BigDecimal(split[1]);
 82                   BigDecimal multiply1 = bd1.multiply(new BigDecimal(100000));
 83                   DecimalFormat dcmFmt1 = new DecimalFormat("0.0");
 84                   //System.out.println(dcmFmt1.format(multiply1));
 85                   du=dcmFmt.format(multiply).substring(0,dcmFmt.format(multiply).length()-2 )+","+dcmFmt1.format(multiply1).substring(0, dcmFmt1.format(multiply1).length()-2);
 86               }else{
 87                   du="00000000,00000000";
 88               }
 89               
 90               //获得速度
 91               String speed;
 92               if(parseObject2.get("speed")==null){
 93                   speed="0";
 94               }else{
 95                   String speed1=parseObject2.get("speed").toString();
 96                   int db3=(int)Double.parseDouble(speed1);
 97                   speed=String.valueOf(db3);
 98               }
 99               
100               //获得方向
101               String head;
102               if(parseObject2.get("direction")!=null){
103                   String head1=parseObject2.get("direction").toString();
104                   int db3=(int)Double.parseDouble(head1);
105                   head=String.valueOf(db3);
106               }else if(parseObject2.get("dir")!=null){
107                   String head1=parseObject2.get("dir").toString();
108                   int db3=(int)Double.parseDouble(head1);
109                   head=String.valueOf(db3);
110               }else{
111                   head="0";
112               }
113               
114               
115               
116               //获得状态
117               String status="1";
118               //获得接收时间
119               String time=UTC;
120               //拼接字符串
121               FileWriter fw = new FileWriter("F:\\mspVisitLog20170223.txt", true);
122               BufferedWriter bw = new BufferedWriter(fw);
123               if(du!="00000000,00000000"){
124               StringBuilder sb = new StringBuilder();
125               sb.append(carID).append(",").append(UTC).append(",").append(du).append(",").append(speed).append(",").append(head).append(",").append(status).append(",").append(time);
126               String message = sb.toString();
127             //bw.append("在已有的基础上添加字符串");
128                bw.write(message+"\r\n");// 往已有的文件上添加字符串
129                //bw.write("\r\n");
130               //System.out.println(message);
131               }
132              
133                
134               line ++;
135               System.out.println(line);
136               bw.close();
137               fw.close();
138          }
139                reader.close();
140         } catch (IOException e) {
141             e.printStackTrace();
142         } finally {
143              
144              if (reader != null) {
145                   try {
146                    reader.close();
147                   } catch (IOException e1) {
148                   }
149              }
150         }
151     }
152     
153     @Test
154     public void WriteStringToFile() {
155         //for(int i=0;i<=5;i++){
156         try {
157             //File file = new File("F:\\test.txt");
158            // PrintStream ps = new PrintStream(new FileOutputStream(file));
159            // ps.println("http://www.jb51.net");// 往文件里写入字符串
160            // ps.append("http://www.jb51.net");// 在已有的基础上添加字符串
161             FileWriter fw = new FileWriter("F:\\test.txt", true);
162             BufferedWriter bw = new BufferedWriter(fw);
163             //bw.append("在已有的基础上添加字符串");
164             bw.write("abc\r\n");// 往已有的文件上添加字符串
165             bw.write("def\r\n");
166             //bw.write("hijk ");
167             bw.close();
168             fw.close();
169         } catch (Exception e) {
170             // TODO Auto-generated catch block
171             e.printStackTrace();
172         }
173         //}
174     }
175 }

 

以上是关于fastjson解析文本的主要内容,如果未能解决你的问题,请参考以下文章

fastjson进行json的解析和序列化

SpringBoot Fastjson解析多层嵌套复杂Json字符串

SpringBoot Fastjson解析多层嵌套复杂Json字符串

解析:JSON 文本没有以数组或对象开头,并且允许未设置片段的选项

fastjson 解析报错 com.alibaba.fastjson.JSONException: create instance error...

java FastJson的使用