1. 先说一个调试程序过程中的心得:
由于txt文本被Editplus处理后编码utf-8的格式好像有BOM之分,于是导致Java文件操作时出现首字节乱码问题。丫的,为什么不尝试新建个txt文件来测试,只是在那一个文件上死扣,调试bug时要注意分析控制变量。
2。解析json时很多乱码,比如出现多个国家的语言的地方。其实这些乱码是由于所用的文本编辑器的解码方式不同而造成的,可以用Editplus,选择一种合适的编码方式即可,这里解析的数据是USA那边的,选择了UTF-8+BOM,便可以解决。由于解析的文档较大,直接处理整个文本太耗内存,而且速度慢,于是每次拷一部分进temp.txt文档,然后对temp.txt文档解析,逐渐解析完毕。
3. 这里的工作介绍如下:知识单纯的读入一个存储JSON格式数据的文本,并将其解析为自定义的格式,然后存入到结果文本中,这篇博客的工作到此结束。之后的工作将文本读取转为一个字符串,并解析建数据库,将数据存入到数据库中,sqlserver2012。
下面是解析的JSON格式:
[ { "Properties": { "ID": [ "com.hyperbeard.muertitos" ], "UpdatedTime": [ "November 11, 2014" ], "ContentRating": [ "Everyone" ], "UpdatedDateByCrawler": [ "20141122" ], "Description": [ "Muertitos (The Little Dead): 这里是Description,内容太长。。。" ], "RatingCount": [ "106" ], "OsVersion": [ "2.3.3 and up" ], "AppSize": [ "41M" ], "NumDownloads": [ "500 - 1,000" ], "Price": [ "$0.99" ], "RatingScore": [ "87.54716873168945" ], "Category": [ "Puzzle" ], "AcquiredDateByCrawler": [ "20141109" ], "PublisherUrl": [ "https://play.google.com/store/apps/developer?id=HyperBeard+Games" ], "Title": [ "Muertitos a Matching Puzzle" ], "Url": [ "https://play.google.com/store/apps/details?id=com.hyperbeard.muertitos" ], "Publisher": [ "HyperBeard Games" ], "CategoryUrl": [ "https://play.google.com/store/apps/category/GAME_PUZZLE" ], "AppVersion": [ "1.1" ], "IconUrl": [ "https://lh5.ggpht.com/s2-q25dSJk6b0tbdebtwspl42Qi95LUzMtCwCCD5vQ0STRWYWf-shYufXFjprRgdWEg=w300" ] } } ]代码:
</pre><p><pre name="code" class="java">package com.shawn.jsontest; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.security.acl.Permission; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class AndroidResolve { public String readFile(String fileName) throws Exception { BufferedReader bf = new BufferedReader(new FileReader(fileName)); String content=""; String s=""; while(content != null){ content = bf.readLine(); if(content == null) break; s=s+content.trim(); } bf.close(); return s; } public static void resolve(String fileIn,String fileOut) throws Exception{ File result = new File(fileOut); BufferedWriter bw = new BufferedWriter(new FileWriter(result,false)); //String string = ; //System.out.println(string.substring(89000, 89900)); JSONArray apps = new JSONArray(new AndroidResolve().readFile(fileIn)); System.out.println(apps.length()); for(int i=0;i<apps.length();i++){ System.out.println("APP\t "+i +"---------------------------------------------------------------------------------------"); bw.write("APP "+String.valueOf(i+1)+":"); JSONObject app = (JSONObject) apps.get(i); JSONObject properties = (JSONObject) app.get("Properties"); JSONArray ID = properties.getJSONArray("ID"); bw.newLine(); bw.write("\tID : "+ID.get(0)); /* System.out.println("APP "+String.valueOf(i+1)+":"); System.out.println("\tID : "+ID.get(0)); */ try { JSONArray UpdateTime = properties.getJSONArray("UpdatedTime"); bw.newLine(); bw.write("\tUpdateTime : "+UpdateTime.get(0)); } catch (Exception e1) { e1.printStackTrace(); } try { JSONArray ContentRating = properties.getJSONArray("ContentRating"); bw.newLine(); bw.write("\tContentRating : "+ContentRating.get(0)); } catch (Exception e2) { // TODO Auto-generated catch block e2.printStackTrace(); } try { JSONArray UpdatedDateByCrawler = properties.getJSONArray("UpdatedDateByCrawler"); bw.newLine(); bw.write("\tUpdatedDateByCrawler : "+UpdatedDateByCrawler.get(0)); } catch (Exception e2) { // TODO Auto-generated catch block e2.printStackTrace(); } try { JSONArray Description = properties.getJSONArray("Description"); bw.newLine(); bw.write("\tDescription : "+Description.get(0)); } catch (Exception e1) { e1.printStackTrace(); } try { JSONArray ScreenshotUrls = properties.getJSONArray("ScreenshotUrls"); for(int j = 0; j < ScreenshotUrls.length(); j++){ bw.newLine(); bw.write("\tScreenshotUrls "+String.valueOf(j)+ ": "+ScreenshotUrls.get(j)); } } catch (Exception e) { //e.printStackTrace(); } try { JSONArray RatingCount = properties.getJSONArray("RatingCount"); bw.newLine(); bw.write("\tRatingCount : "+RatingCount.get(0)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } try { JSONArray OsVersion = properties.getJSONArray("OsVersion"); bw.newLine(); bw.write("\tOsVersion : "+OsVersion.get(0)); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { JSONArray AppSize = properties.getJSONArray("AppSize"); bw.newLine(); bw.write("\tAppSize : "+AppSize.get(0)); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { JSONArray NumDownloads = properties.getJSONArray("NumDownloads"); bw.newLine(); bw.write("\tNumDownloads : "+NumDownloads.get(0)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } try { JSONArray Price = properties.getJSONArray("Price"); bw.newLine(); bw.write("\tPrice : "+Price.get(0)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } try { JSONArray RatingScore = properties.getJSONArray("RatingScore"); bw.newLine(); bw.write("\tRatingScore : "+RatingScore.get(0)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } try { JSONArray Category = properties.getJSONArray("Category"); bw.newLine(); bw.write("\tCategory : "+Category.get(0)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } try { JSONArray AcquiredDateByCrawler = properties.getJSONArray("AcquiredDateByCrawler"); bw.newLine(); bw.write("\tAcquiredDateByCrawler : "+AcquiredDateByCrawler.get(0)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } try { JSONArray PublisherUrl = properties.getJSONArray("PublisherUrl"); bw.newLine(); bw.write("\tPublisherUrl : "+PublisherUrl.get(0)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } try { JSONArray Title = properties.getJSONArray("Title"); bw.newLine(); bw.write("\tTitle : "+Title.get(0)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } try { JSONArray Url = properties.getJSONArray("Url"); bw.newLine(); bw.write("\tUrl : "+Url.get(0)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } try { JSONArray Publisher = properties.getJSONArray("Publisher"); bw.newLine(); bw.write("\tPublisher : "+Publisher.get(0)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } try { JSONArray CategoryUrl = properties.getJSONArray("CategoryUrl"); bw.newLine(); bw.write("\tCategoryUrl : "+CategoryUrl.get(0)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } try { JSONArray AppVersion = properties.getJSONArray("AppVersion"); bw.newLine(); bw.write("\tAppVersion : "+AppVersion.get(0)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } try { JSONArray IconUrl = properties.getJSONArray("IconUrl"); bw.newLine(); bw.write("\tIconUrl : "+IconUrl.get(0)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } bw.newLine(); } bw.close(); } public static void main(String[] args){ try { resolve("E:\\JsonResolve\\temp2.txt", "E:\\JsonResolve\\temp2_result.txt"); System.out.println("Done"); } catch (Exception e) { e.printStackTrace(); } } }