我有xml文件,我需要转换为json。 json输出是xml的部分转换,而不是整个xml被转换..
您可以找到json输出的原因,只有部分正在转换。 剩下的部分被省略了。
<?xml version="1.0"?>
<ASF_Service_ResponseVO typesig="155c0afe34b" id="1">
<service type="String">OnboardingV2</service>
<operation type="String">start_onboarding_session</operation>
<requested_version type="String">1.0</requested_version>
<actual_version type="String">1.0</actual_version>
<server_info type="String">onboardingv2serv:start_onboarding_session&CalThreadId=85&TopLevelTxnStartTime=13b40fe91c4&Host=L-BLR-00438534&pid=3564</server_info>
<result typesig="fff96cab366a5def" type="Onboarding::StartOnboardingSessionResponse" id="2">
<onboarding_id type="String">137</onboarding_id>
<success type="bool">true</success>
</result>
</ASF_Service_ResponseVO>
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javasample1;
//import net.sf.json.JSON;
//import net.sf.json.JSONObject;
//import net.sf.json.xml.XMLSerializer;
import org.json.JSONObject;
import org.json.JSONException;
import org.json.XML;
import java.io.*;
/**
*
* @author gopc
*/
public class ConvertXMLtoJSON{
public static void main(String[] args) throws Exception
{
try
{
File file = new File ("C:\\Users\\gopc\\Documents\\NetBeansProjects\\JavaSample1\\src\\javasample1\\Sample1.xml");
InputStream inputStream = new FileInputStream(file);
StringBuilder builder = new StringBuilder();
int ptr = 0;
while ((ptr = inputStream.read()) != -1 )
{
builder.append((char) ptr);
}
String xml = builder.toString();
JSONObject jsonObj = XML.toJSONObject(xml);
System.out.println(jsonObj);
/*
String xmlString = "<?xml version=\"1.0\"?><ASF_Service_ResponseVO id=\"1\"><service type=\"String\">OnboardingV2</service><operation type=\"String\">start_onboarding_session</operation><requested_version type=\"String\">1.0</requested_version><actual_version type=\"String\">1.0</actual_version><server_info type=\"String\">onboardingv2serv:start_onboarding_session&CalThreadId=85&TopLevelTxnStartTime=13b40fe91c4&Host=L-BLR-00438534&pid=3564</server_info><result type=\"Onboarding::StartOnboardingSessionResponse\" id=\"2\"><onboarding_id type=\"String\">137</onboarding_id><success type=\"bool\">true</success></result></ASF_Service_ResponseVO>";
JSONObject jsonObj = XML.toJSONObject(xmlString);
System.out.println(jsonObj.toString());
*/
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
run:
{"ASF_Service_ResponseVO":{"server_info":{"content":"onboardingv2serv:start_onboarding_session&CalThreadId=85&TopLevelTxnStartTime=13b40fe91c4&Host=L-BLR-00438534&pid=3564","type":"String"},"result":{"id":2,"typesig":"fff96cab366a5def","onboarding_id":{"content":137,"type":"String"},"type":"Onboarding::StartOnboardingSessionResponse","success":{"content":true,"type":"bool"}},"operation":{"content":"start_onboarding_session","type":"String"},"requested_version":{"content":1,"type":"String"},"service":{"content":"OnboardingV2","type":"String"},"actual_version":{"content":1,"type":"String"}}}
建立成功(总时间:0秒)
生成的JSON对象:
{
"ASF_Service_ResponseVO": {
"server_info": {
"content":"onboardingv2serv:start_onboarding_session&CalThreadId=85&TopLevelTxnStartTime=13b40fe91c4&Host=L-BLR-00438534&pid=3564",
"type":"String"
},
"result": {
"id":2,
"typesig":"fff96cab366a5def",
"onboarding_id": {
"content":137,
"type":"String"
},
"type":"Onboarding::StartOnboardingSessionResponse",
"success": {
"content":true,
"type":"bool"
}
},
"operation": {
"content":"start_onboarding_session",
"type":"String"
},
"requested_version":{
"content":1,
"type":"String"
},
"service":{
"content":"OnboardingV2",
"type":"String"},
}
"actual_version":{
"content":1,
"type":"String"
}
}
}
答案 0 :(得分:0)
如果您不期望xml的顺序相同,则以下是输出json
{
"ASF_Service_ResponseVO":{
"server_info":{
"content":"onboardingv2serv:start_onboarding_session&CalThreadId=85&TopLevelTxnStartTime=13b40fe91c4&Host=L-BLR-00438534&pid=3564",
"type":"String"
},
"id":"1",
"result":{
"id":"2",
"typesig":"fff96cab366a5def",
"onboarding_id":{
"content":"137",
"type":"String"
},
"type":"Onboarding::StartOnboardingSessionResponse",
"success":{
"content":"true",
"type":"bool"
}
},
"operation":{
"content":"start_onboarding_session",
"type":"String"
},
"requested_version":{
"content":"1.0",
"type":"String"
},
"typesig":"155c0afe34b",
"service":{
"content":"OnboardingV2",
"type":"String"
},
"actual_version":{
"content":"1.0",
"type":"String"
}
}
}