简体   繁体   中英

Unable to append “,” after key-value pair in JSON file when i am converting from XML to Json using java

I am trying to convert xml to json and writing this json to a file,in the log am getting proper result in the form of key value pair since logs and prints deals with String but durin run-time the same is not getting reflected as no "," is getting appended after value

    String xml = builder.toString();
    ObjectMapper mapper = new ObjectMapper();
    JSONObject jsonObj = XML.toJSONObject(xml,false);
    FileWriter fileWriter =new FileWriter(outputFileName);
    BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);  
    Object json = mapper.readValue(jsonObj.toString(), Object.class);
    ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
    writer.writeValue(new File(outputFileName), json);`

The log ouptput is

{
  "cobrandCardPopup" : {
    "linkClickId" : "T10_AirActiveNeedsNonAirOnboarding_HasCardSE_HighMilesBalance",
    "linkLinkStatus" : "_self",
    "LinkTitleAttr" : "",
    "linkFollowCheckbox" : "",
    "linkURL" : "http://hotels.jetprivilege.com",
    "LinkAriaLabel" : "",
    "ToasterImageContainer" : {
      "ToasterMobileImage" : "/iwov-resources/images/splash-popups/Hotel_Mob.gif",
      "ToasterImage" : "/iwov-resources/images/splash-popups/Hotel.gif",
      "ToasterMobileImageAltText" : "Hotel",
      "ToasterImageAltText" : "Hotel"
    },
    "LinkRoleAttr" : "",
    "ToasterTextContainer" : {
      "popupInterval" : "",
      "linkText" : "",
      "popupText" : ""
    },
    "ToasterType" : "Image"
  }
}

The file has

{
  "cobrandCardPopup" : {
    "linkClickId" : "T10_AirActiveNeedsNonAirOnboarding_HasCardSE_HighMilesBalance"
    "linkLinkStatus" : "_self"
    "LinkTitleAttr" : ""
    "linkFollowCheckbox" : ""
    "linkURL" : "http://hotels.jetprivilege.com"
    "LinkAriaLabel" : ""
    "ToasterImageContainer" : {
      "ToasterMobileImage" : "/iwov-resources/images/splash-popups/Hotel_Mob.gif"
      "ToasterImage" : "/iwov-resources/images/splash-popups/Hotel.gif"
      "ToasterMobileImageAltText" : "Hotel"
      "ToasterImageAltText" : "Hotel"
    },
    "LinkRoleAttr" : ""
    "ToasterTextContainer" : {
      "popupInterval" : ""
      "linkText" : ""
      "popupText" : ""
    },
    "ToasterType" : "Image"
  }
}

You should do as little data conversion as possible to 1. simplify the code and 2. reduce the error possibilities

You could do either: do not use the jackson objectmapper at all as you are already mapping to JsonObject

String xml = builder.toString();
JSONObject jsonObj = XML.toJSONObject(xml);
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(outputFileName));
bufferedWriter.write(jsonObj.toString())

Or you can use the fileWriter of jackson directly:

ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
writer.writeValue(new File(outputFileName), jsonObj);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM