gpt4 book ai didi

java - 无法使用java将xml转换为json

转载 作者:行者123 更新时间:2023-12-01 04:58:51 25 4
gpt4 key购买 nike

我有 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&amp;CalThreadId=85&amp;TopLevelTxnStartTime=13b40fe91c4&amp;Host=L-BLR-00438534&amp;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>

ConvertXMLtoJSON.java

/*
* 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&amp;CalThreadId=85&amp;TopLevelTxnStartTime=13b40fe91c4&amp;Host=L-BLR-00438534&amp;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"
}
}
}

最佳答案

如果不希望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"
}
}
}

关于java - 无法使用java将xml转换为json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13641368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com