json xml converter having array in json string

前端 未结 4 1672
余生分开走
余生分开走 2021-01-25 15:07

I need to convert a json to xml and later on converting that json back to xml, but i am loosing the json array object while this conversion - I am using org.json lib.

4条回答
  •  失恋的感觉
    2021-01-25 15:45

    Json specification says that json can have datatypes like string,number, boolean, array, object and null.

    single element array in json is a special case which gives problem with some api like org.json. Because these library treat single element array as normal json object and not an json array object.

    To solve this problem we need a library which support all the datatypes of a Json. We can use xslt transformation to solve this problem. In XSLT 3.0 there are 2 funtions json-to-xml() and xml-to-json() which works perfect for this problem. I have solved similar problem using this way. These functions transform json to xml and vice versa considering all the data type of json. for eg. In a json if you give value as number, boolean or string library will maintain these data types at the time of converstion (xml to json or vice versa so that after conversion in xml you can see particular datatype of a value in genrated xml). We can see in genrated xml that any key in json is always string type.

    More on below link xslt 3.0 json-to-xml and xml-to-json conversion

    Check both conversion examples on below xsltfiddle links

    Json to xml convertion

    https://xsltfiddle.liberty-development.net/6qVRKvS

    get back Json from previous xml

    https://xsltfiddle.liberty-development.net/94hvTyU

    Below is a complete java example of this conversion using XSLT and Saxon 9.8 HE (HE means home edition. It's open source product provides implementations of XSLT (3.0)

    First you need to run json2XML.java which will genrate outputSampleXML.xml. This outputSample.XML will have conversion from json to xml and it will be input of our next program named xml2json.java. This program will give you back your original json containing single array element.

    pom.xml dependency

        
            net.sf.saxon
            Saxon-HE
            9.8.0-8
        
    
        
            com.google.code.gson
            gson
            2.8.2
        
    

    inputJSON.xml

    
    {
        "Request": [{
            "price": "1400",
            "test": "dummydata"
        }]
    }
    
    

    json2xml.xsl

    
        
        
            
        
    
    

    json2XML.java

    public class json2XML {
    public static void simpleTransform(String sourcePath, String xsltPath, String resultDir) {
        TransformerFactory tFactory = TransformerFactory.newInstance();
        try {
            Transformer transformer = tFactory.newTransformer(new StreamSource(new File(xsltPath)));
            transformer.transform(new StreamSource(new File(sourcePath)), new StreamResult(new File(resultDir)));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        simpleTransform("src/main/java/sampleJSON.xml", "src/main/java/json2xml.xsl",
                "src/main/java/outputSampleXML.xml");
        System.out.println("Done!!!");
    
    }
    

    }

    outputSampleXML.xml

    
    
       
          
             1400
             dummydata
          
       
    
    

    xml2json.xsl

    
      
      
          
      
    
    

    xml2JSON.java

    public class json2XML {
    public static void simpleTransform(String sourcePath, String xsltPath, String resultDir) {
        TransformerFactory tFactory = TransformerFactory.newInstance();
        try {
            Transformer transformer = tFactory.newTransformer(new StreamSource(new File(xsltPath)));
            transformer.transform(new StreamSource(new File(sourcePath)), new StreamResult(new File(resultDir)));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        simpleTransform("src/main/java/sampleJSON.xml", "src/main/java/json2xml.xsl",
                "src/main/java/outputSampleXML.xml");
        System.out.println("Done!!!");
    
    }
    

    }

    Output on Console on running xml2JSON.java

    {
        "Request": [{
            "price": "1400",
            "test": "dummydata"
        }]
    }
    

    Note you can keep both the xsl file in a java String object as a constant and use them as inputStream in transformer java code. So that you won't need to make seprate xsl files. But that is other part of problem and need not to concentrate here.

提交回复
热议问题