想要将xml String转换为Json,我正如下所示。
必须转换的XML
<Item>
<Property name="Description" value="Description 1"/>
<Property name="EffDate" value="01/05/2017"/>
<Property name="ExpDate" value="12/31/9999"/>
<Property name="Status" value="Launched"/>
</Item>
我已经为xml创建了一个Class,如下所示。
public class Context {
@XmlElement(name = "Item")
private List<Item> offer;
}
public class Item {
@XmlElement(name = "Property")
private List<Property> properties;
}
public class Property {
@XmlAttribute
private String name;
@XmlAttribute
private String value;
}
我正在使用Gson库将此Java对象转换为Json - g.toJson。
Comverted JSON -
"offer": [{
"properties": [{
"name": "Description",
"value": "Description 1"
},
{
"name": "EffDate",
"value": "01/05/2017"
},
{
"name": "ExpDate",
"value": "12/31/9999"
},
{
"name": "Status",
"value": "Launched"
}]
}]
但我们想将JSON转换为如下 -
"offer": [{
"Description" : "Description 1",
"EffDate":"01/05/2017",
"ExpDate": "12/31/9999",
"Status": "Launched"
}]
有没有办法将属性名称和值转换为Item类属性。?
答案 0 :(得分:0)
尝试使用此链接:https://github.com/stleary/JSON-java这是一个JSON Helper类,可以将XML转换为JSON,例如:
public class Main {
public static int PRETTY_PRINT_INDENT_FACTOR = 4;
public static String TEST_XML_STRING =
"<?xml version=\"1.0\" ?><test attrib=\"moretest\">Turn this to JSON</test>";
public static void main(String[] args) {
try {
JSONObject xmlJSONObj = XML.toJSONObject(TEST_XML_STRING);
String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
System.out.println(jsonPrettyPrintString);
} catch (JSONException je) {
System.out.println(je.toString());
}
}
}
希望这会有所帮助:)
答案 1 :(得分:0)
可以使用FasterXML库。您可以在其中编写用于生成XML和JSON的自定义逻辑。通过覆盖serialize
类的JsonSerializer
。
需要编写如下序列化程序:
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
public class ContextSerializer extends JsonSerializer<Context> {
@Override
public void serialize(Context t, JsonGenerator jg, SerializerProvider sp) throws IOException, JsonProcessingException {
jg.writeStartObject();
jg.writeArrayFieldStart("offer");
for (Item i : t.offer) {
jg.writeStartObject();
for (Property property : i.properties) {
jg.writeStringField(property.name, property.value);
}
jg.writeEndObject();
}
jg.writeEndArray();
jg.writeEndObject();
}
}
转换:
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBException;
public class Main {
public static void main(String[] args) throws JAXBException, JsonProcessingException {
Context c = new Context();
List<Item> offer = new ArrayList<>();
Item pr = new Item();
pr.properties = new ArrayList<>();
Property p = new Property();
p.name = "asdf";
p.value = "va1";
pr.properties.add(p);
p = new Property();
p.name = "asdf1";
p.value = "va11";
pr.properties.add(p);
offer.add(pr);
c.offer = offer;
try {
SimpleModule module = new SimpleModule();
module.addSerializer(Context.class, new ContextSerializer());
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(module);
objectMapper.setSerializationInclusion(Include.NON_DEFAULT);
String json = objectMapper.writeValueAsString(c);
System.out.println(json);
} catch (Exception e) {
System.out.println(""+e);
}
}
}
O / P JSON:(如果您将名称提供给列表(&#34; offer&#34;),则提示O / P JSON在您的问题中是错误的,那么它总是在对象内{ {3}})
{
"offer": [{
"asdf": "va1",
"asdf1": "va11"
}
]
}
包的Maven依赖关系是:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0.pr3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0.pr3</version>
</dependency>
答案 2 :(得分:0)
如果您使用的是 Java 8 或更高版本,则应该查看我的开源库:unXml。 unXml基本上从Xpath映射到Json属性。
可在Maven Central上找到。
实施例
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.nerdforge.unxml.factory.ParsingFactory;
import com.nerdforge.unxml.parsers.Parser;
import org.w3c.dom.Document;
public class Parser {
public ObjectNode parseXml(String xml){
Parsing parsing = ParsingFactory.getInstance().create();
Document document = parsing.xml().document(xml);
Parser<ObjectNode> parser = parsing.obj("/")
.attribute("offer", parsing.arr("/Item")
.attribute("Description", "Property[@name='Description']/@value")
.attribute("EffDate", "Property[@name='EffDate']/@value")
.attribute("ExpDate", "Property[@name='ExpDate']/@value")
.attribute("Status", "Property[@name='Status']/@value")
)
.build();
ObjectNode result = parser.apply(document);
return result;
}
}
它将返回Jackson ObjectNode
,其中包含以下json:
{
"offer": [
{
"Status": "Launched",
"Description": "Description 1",
"ExpDate": "12/31/9999",
"EffDate": "01/05/2017"
}
]
}
答案 3 :(得分:0)
您可以将xml转换为地图,对其进行修改,然后转换为json。 Underscore-java库具有静态方法U.fromXml(xml)
和U.toJson(json)
。我是该项目的维护者。