In Search and Promote I have presentation template which response xml.
Now I need get response in json format. It is enough hard to find analogs of xml tags for json because this template is huge.
Is there more elegant way?
In Search and Promote I have presentation template which response xml.
Now I need get response in json format. It is enough hard to find analogs of xml tags for json because this template is huge.
Is there more elegant way?
1st thing you can create a custom template for json response in S&P. refer to the current documentation hereand documentation
Alternatively you can achieve this by creating a servlet which will consume the response.xml and will provide you response in json format.
To convert XML File in to JSON include the following dependency
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>
Then implement as:
String xmlString = "<xml>yourStringURLorFILE</xml>";
JSONObject jsonObject = XML.toJSONObject(xmlString);
System.out.println(jsonObject);
The only problem with JSON in Java is that if your XML has a single child, but is an array, it will convert it to an object instead of an array. This can cause problems if you dynamically always convert from XML to JSON, where if your example XML has only one element, you return an object, but if it has 2+, you return an array, which can cause parsing issues for people using the JSON. this can be solved by by writing a "convertToArray()" method , which returns an array with the single object in it. So , you can always trust your value to be an array.