我在使用GSON库将源xml转换为JSON时遇到问题。请在下面的代码,源xml和输出中找到。我正在SAP PI的Java映射中实现此目标,在该映射中,我正在获取源xml作为对此Java代码的输入。
源XML:
<data>
<ERP>0080001</ERP>
<Shiptoparty>0088000</Shiptoparty>
<Shippingpoint>503</Shippingpoint>
<Issuedate>20181102</Issuedate>
<products>
<Unit>L</Unit>
<QTY>2.000 </QTY>
<SKUno>000000000011</SKUno>
</products>
<products>
<Unit>L</Unit>
<QTY>0.000 </QTY>
<SKUno>000000000011000078</SKUno>
</products>
</data>
代码:
package xmlgsonjson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
public class MakeItJSONByGSON extends AbstractTransformation
{
public String targetfile ="";
public MakeItJSONByGSON() {}
public void transform(TransformationInput in, TransformationOutput out) throws StreamTransformationException
{
try {
String sourcexml = "";
String line = "";
InputStream inputstream = in.getInputPayload().getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputstream));
OutputStream outputstream = out.getOutputPayload().getOutputStream();
byte[] b = null;
try {
b = new byte[inputstream.available()];
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
inputstream.read(b);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String inputContent = new String(b);
sourcexml = inputContent;
try {
while ((line = br.readLine()) != null) {
sourcexml = sourcexml + line;
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String jsonPrettyPrintString = gson.toJson(sourcexml);
targetfile = jsonPrettyPrintString;
out.getOutputPayload().getOutputStream().write(targetfile.getBytes());
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
输出:
"\u003cdata\u003e\n \u003cERP\u003e0080001\u003c/ERP\u003e\n \u003cShiptoparty\u003e0088000\u003c/Shiptoparty\u003e\n \u003cShippingpoint\u003e503\u003c/Shippingpoint\u003e\n \u003cIssuedate\u003e20181102\u003c/Issuedate\u003e\n \u003cproducts\u003e\n \u003cUnit\u003eL\u003c/Unit\u003e\n \u003cQTY\u003e2.000 \u003c/QTY\u003e\n \u003cSKUno\u003e000000000011\u003c/SKUno\u003e\n \u003c/products\u003e\n \u003cproducts\u003e\n \u003cUnit\u003eL\u003c/Unit\u003e\n \u003cQTY\u003e0.000 \u003c/QTY\u003e\n \u003cSKUno\u003e000000000011000078\u003c/SKUno\u003e\n \u003c/products\u003e\n \u003c/data\u003e"
预期输出:
{"data": [
{
"ERP": 0080001,
"Issuedate": 20181102,
"Shippingpoint": 503,
"Shiptoparty": 0088000,
"products": [
{
"Unit": "L"
"QTY": 2.000,
"SKUno": 000000000011,
},
{
"Unit": "L"
"QTY": 0.000,
"SKUno": 000000000011000078,
}
]
}
]}
请让我知道如何解决此问题。
我认为您在编码方面遇到问题。可能的解决方法是
String jsonString = new Gson().toJson(objectToEncode);
byte[] utf8JsonString = jsonString.getBytes("UTF8");
responseToClient.write(utf8JsonString, 0, utf8JsonString.Length);
我希望这对您有用:)