高性能JSON 格式转换代码

xiaoxiao2021-02-28  59

不同的代码,性能差别很到,

so there you have it. If you’re concerned about parsing speedfor your JSON library, choose Jackson for big files, GSON for small files, andJSON.simple for handling both.

下面是性能比较好的参考代码

import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.HashMap; import java.util.Iterator; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.apache.commons.io.IOUtils; public class Partner2Contact {     public Partner2Contact() {         super();     }               public static  String partner2contact(String json) {         JSONObject jo = JSONObject.fromObject(json);         JSONArray jsonArray = (JSONArray) jo.get("Partners");                  JSONObject outerObject = new JSONObject();         JSONArray outerArray = new JSONArray();         JSONObject innerObject = new JSONObject();         JSONArray innerArray = new JSONArray();         JSONObject phoneObject = new JSONObject();         for (int i = 0; i < jsonArray.size(); ++i) {                         JSONObject o = (JSONObject) jsonArray.get(i);                        // System.out.println("name:" + o.getString("name") + "," + "home:"                        //         + o.getString("home") + " home phone:" + o.getString("home_phone"));                      String name[] =o.getString("name").split(",");             String home[] =o.getString("home").split(",");             innerObject.put("Last_Name", name[1]);             innerObject.put("First_Name", name[0]);             innerObject.put("Address", home[0]);             innerObject.put("City", home[1]);             innerObject.put("State", home[2]);             innerObject.put("Zip_Plus_4", o.getString("zip"));                          phoneObject.put("type","H");             phoneObject.put("number",o.getString("home_phone"));             innerArray.add(phoneObject);                          phoneObject.put("type","O");             phoneObject.put("number",o.getString("office_phone"));             innerArray.add(phoneObject);                          phoneObject.put("type","C");             phoneObject.put("number",o.getString("cell_phone"));             innerArray.add(phoneObject);                         innerObject.put("Phones", innerArray);             innerArray.clear();             outerArray.add(innerObject);                    }                  outerObject.put("Contacts", outerArray);         outerArray.clear();         return outerObject.toString();     }       public static void main(String[] args) {         Partner2Contact partner2Contact = new Partner2Contact();                                                   File file = new File("D:\\rhosb\\src\\JSONConvert\\4k.json");                                         try {                                         InputStream inputStream = new FileInputStream(file);                     InputStream is = new FileInputStream(file);                     String json = IOUtils.toString(is);                     System.out.println("json string========" + json);                     String str=partner2Contact.partner2contact(json);                     System.out.println("return:" + str);                                      } catch (IOException e) {                     e.printStackTrace();                 }                      } }
转载请注明原文地址: https://www.6miu.com/read-72876.html

最新回复(0)