Fastjson parse text and fastjson parse

Source: Internet
Author: User

Fastjson parse text and fastjson parse

Json format in text

20170215 18:00:00. 001-{"visitTime": "1487152800001", "ip": "xx. xxx. xxx. xxx "," params ":" {\ "encode \": \ "UTF-8 \", \ "xxx \": \ "0.0 \", \ "xxxx \": \ "6869679 \", \ "resType \": \ "json \", \ "ak \": \ "xxxxxxxxxxxxxxxxxxxxx \", \ "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 "}

Code (Alibaba fastjson package is used)

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 ("read the file content in the unit of action, one read for the whole row at a time:"); 29 reader = new Buf FeredReader (new FileReader (file); 30 String tempString = null; 31 int line = 1; 32 // read a row at a time, until null is read, the end of the file is 33 while (tempString = reader. readLine ())! = Null) {34 // display the row number 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 // active vehicle id 41 String carID; 42 if (parseObject2.get ("vhcid") = null) {43 String ip = parseObject. get ("ip "). toString (); 44 String carID1 = MD5Utils. md5 (I P ). 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 // obtain the positioning time 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 // obtain latitude Degrees and longitude 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 BigD Ecimal (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 BigDec Imal (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 = "average hour, cost 0000"; 88} 89 90 // get speed 91 String speed; 92 if (parseObject2.get ("speed ") = null) {93 spee D = "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 // obtain the direction 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 // get status 117 String status = "1 "; 118 // obtain the receipt time 119 String time = UTC; 120 // concatenate the String 121 FileWriter fw = new FileWriter ("F: \ mspVisitLog20170223.txt", true ); 122 BufferedWriter bw = new BufferedWriter (fw); 123 if (du! = "Random points, 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 ("adding strings based on existing"); 128 bw. write (message + "\ r \ n"); // Add a string 129 // bw to an existing file. write ("\ r \ n"); 130 // System. out. println (mescript E); 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 @ Test154 public void WriteStringToFile () {153 // 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"); // write the string to the file 160 // ps. append ("http://www.jb51.net"); // Add the string 161 FileWriter fw = new FileWriter ("F: \ test.txt", true) on an existing basis ); 162 BufferedWriter bw = new BufferedWriter (fw); 163 // bw. append ("adding strings based on existing"); 164 bw. write ("abc \ r \ n"); // Add a 165 bw string to an existing file. write ("def \ r \ n"); 166 // bw. write ("hijk"); 167 bw. close (); 168 fw. close (); 169} catch (Exception e) {170 // TODO Auto-generated catch block171 e. printStackTrace (); 172} 173 //} 174} 175}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.