XML到Json转换:数据顺序

时间:2014-06-02 16:04:45

标签: java xml json

我使用Java应用程序将XML文件转换为json。我的xml从excel表导出,其中数据按以下顺序显示在列中:2013,2014,2015,2016,2017

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import org.json.XML;
import org.json.JSONException;
import org.json.JSONObject;
/*import javax.xml.parsers.*;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;*/

@SuppressWarnings("unused")
public class XMLConverter {

    public static int PRETTY_PRINT_INDENT_FACTOR=4;
    public static String fileContent="";
    public static void main(String[] args) throws Exception{
    BufferedReader xmlReader= new BufferedReader(new FileReader("test.xml"));
    BufferedWriter jsonFile=new BufferedWriter(newFileWriter("converted.json"));
    String lineContent="";


        while (lineContent !=null){
            lineContent=xmlReader.readLine();
            fileContent+=lineContent;

        }


        try{
            org.json.JSONObject xmlJSONObj=XML.toJSONObject(fileContent);
            String jsonScript = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
            jsonFile.write(jsonScript); 
            //System.out.println(jsonScript);

        }catch (JSONException exc){
            System.out.println(exc.toString());
        }



        xmlReader.close();
        jsonFile.close();
    }

}

代码运行没有问题,我得到一个大部分正确的转换文件,但其中的数据不是它应该的顺序:我得到类似的东西:             " 2013":45,             " 2015":40.61,             " 2014":42.75,             " 2017":36.65,             " 2016":38.58

另外,如果我使用带有两个表的文件,则表顺序被反转

0 个答案:

没有答案