json字符串与xml之间转换 json数组与集合之间转换

 

 

 

 

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.*;
import org.dom4j.io.XMLWriter;

 

 

public class Demo {


 private String firstName;
 private String lastName;
 private String email;
 private static String jsonString = "{ \"programmers\": [{ \"firstName\": \"Elliotte\", \"lastName\":\"Harold\",\"email\": \"
[email protected]\" }],"
   + " \"authors\": [{ \"firstName\": \"Isaac\", \"lastName\": \"Asimov\", \"genre\": \"science fiction\" }],"
   + " \"musicians\": [{ \"firstName\": \"Eric\", \"lastName\": \"Clapton\", \"instrument\": \"guitar\" },"
   + "  { \"firstName\": \"Sergei\", \"lastName\": \"Rachmaninoff\", \"instrument\": \"piano\" }] }";

 

 

public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public String getEmail() {
  return email;
 }

 public void setEmail(String email) {
  this.email = email;
 }

 

 

/**
  * JsonArray to List
  *
  * @param <T>
  * @param obj
  * @param clazz
  * @return
  */
 public static <T> List<T> JsonToList(Object obj, Class clazz) {

  JSONObject jsonObj = JSONObject.fromObject(jsonString);
  List<T> list = new ArrayList<T>();
  for (Iterator iter = jsonObj.keys(); iter.hasNext();) {
   String key = (String) iter.next();
   JSONArray array = jsonObj.getJSONArray(key);

   for (int i = 0; i < array.size(); i++) {
    JSONObject object = (JSONObject) array.get(i);
    T t = (T) JSONObject.toBean(object, clazz);
    if (t != null)
     list.add(t);
   }
  }
  return list;
 }

 

 

 public static void JsonToXML() {
  JSONObject jsonObj = JSONObject.fromObject(jsonString);
  XMLSerializer xmlSerializer = new XMLSerializer();
  String xmlObject = xmlSerializer.write(jsonObj);
  try {
   Document document = DocumentHelper.parseText(xmlObject);
   XMLWriter writer = new XMLWriter(new FileWriter(
     "../json/src/demo2.xml"));
   writer.write(document);
   writer.close();
  } catch (DocumentException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  System.out.println(xmlObject);
 }

 

 

 public static void XMLToJson() {
  XMLSerializer xmlSerializer = new XMLSerializer();
  JSON json = xmlSerializer
    .readFromFile(new File("src\\Dom2jSample.xml"));
  System.out.println(json.toString());
 }

 

 

 public static void CreatXML() {
  Demo demo = new Demo();
  Document document = demo.createDocument();
  /**
   * way 1
   */
  try {
   demo.FileWrite(document);
  } catch (IOException e1) {
   e1.printStackTrace();
  }
  // Element legend = demo.FindElement(document);
  // System.out.println(legend.getText());
  /**
   * way 2
   */
  try {
   Document documentStr = demo.StringToXML("<China>I Love!</China>");
   demo.XMLWrite(documentStr);
  } catch (Exception e) {
  }

 }

 

 

 

 /*
  * Create a XML Document
  */
 public Document createDocument() {
  Document document = DocumentHelper.createDocument();

  Element root = document.addElement("root");

  Element author1 = root.addElement("Lynch");
  author1.addAttribute("Age", "25");
  author1.addAttribute("Country", "China");
  author1.addText("I am great!");

  Element author2 = root.addElement("Legend");
  author2.addAttribute("Age", "25");
  author2.addAttribute("Country", "China");
  author2.addText("I am great!too!");

  return document;
 }

 

 

 

 /*
  * Create a XML document through String
  */
 public Document StringToXML(String str) throws DocumentException {
  Document document = DocumentHelper.parseText(str);
  return document;
 }

 

 

 

 public Element FindElement(Document document) {
  Element root = document.getRootElement();
  Element legend = null;
  for (Iterator i = root.elementIterator("legend"); i.hasNext();) {
   legend = (Element) i.next();
  }
  return legend;
 }

 

 

 

 /*
  * Write a XML file
  */
 public void FileWrite(Document document) throws IOException {
  FileWriter out = new FileWriter("../json/src/demo.xml");
  document.write(out);
  out.close();
 }

 

 

 

 /*
  * Write a XML format file
  */
 public void XMLWrite(Document document) throws IOException {
  XMLWriter writer = new XMLWriter(
    new FileWriter("../json/src/demo2.xml"));
  writer.write(document);
  writer.close();
 }

 public static void main(String[] args) {
  // List<Demo> list = JsonToList(new Demo(), Demo.class);
  // for (Demo demo : list) {
  // System.out.println(demo.getFirstName());
  // }
  // XMLToJson();
  JsonToXML();
  CreatXML();
 }

}

  

你可能感兴趣的:(json,xml,.net)