【问题标题】:Good way to "keep" newline character in JSON?在 JSON 中“保留”换行符的好方法?
【发布时间】:2015-10-15 08:14:50
【问题描述】:

我正在构建一个 Java Jersey 2 Rest API(爬虫)。爬虫从文章中检索文本并以 json 格式返回。该 API 稍后将被其他开发人员使用。

所以我的问题是 - 如何在我的 json 输出中的字符串中“保留”换行符?

处理此问题的另一个服务示例(json 输出):

"text": "When Etsy bought Grand St. last April, . But that’s about to change. At the end of this month, Grand St. will stop processing orders and on October 1, listings on the site will become inactive. At that point, Grand St.’s site will just be a gallery of content and photos.\nThat’s because the Grand St. team has taken on a lot more projects at Etsy, so from a user experience and maintenance perspective, it made sense to move Grand St. away from commerce, Etsy Senior PR Manager Nicole Summer told TechCrunch.\n“We at Etsy and Grand St. have truly appreciated all the hard work from Grand St. makers, and we welcome them to learn more about joining the Etsy community,” Summer said. “The Grand St. team has become an integral part of the Etsy organization, working on innovative projects to help our sellers scale. We’re grateful to have them on board and excited to continue the work we’re doing to empower our sellers to achieve their creative business goals on their own terms.”\nFor background, Grand St. sells things like solar chargers, a smart light for nighttime bike rides and The Cash Cannon for making it rain. Before the acquisition, Grand St. had raised $1.3 million in seed funding from First Round Capital, David Tisch, Gary Vaynerchuk, betaworks, Collaborative Fund, MESA+, Quotidian Ventures, and Undercurrent.\nFeatured Image: Dennis Skley/Flickr UNDER A CC BY-ND 2.0 LICENSE",

您可以看到他们在 json 中使用了 \n。

【问题讨论】:

  • 我认为 '\n'(标准方式)是从服务传递 json 换行符的好方法,客户端理解它并从那里中断语句。
  • 谢谢 :) 问题是,JSON 解析器 (jackson) 从字符串 (\n) 中删除了这些字符。任何提示如何防止这种情况?
  • 在下面查看我的答案,如果它不起作用,请告诉我。

标签: java json jersey-2.0


【解决方案1】:

我也在使用 Jackson 并按照如下所示的方式进行操作(仅在此处粘贴特定部分)。 对我来说一切正常,请根据您的需要进行更改。

@GET
@Path("/getLocale")
@Produces({ MediaType.APPLICATION_JSON} )
public Response getLocale(@Context HttpServletRequest request) {
    ResponseBuilder responseBuilder = Response.status(200);

    String str=null;
    byte[] bytes=null;

    String appLocation = SystemParameters.getInstance().getParameter("application.home.dir")+"/WEB-INF/lang/";

    InputStream is = new FileInputStream(appLocation+"/en_US.json");

    try {
        bytes = IOUtils.toByteArray(is);
    } catch (IOException e) {
        //ERROR
    }

    str = new String(bytes, "UTF-8");
    str = str.replaceAll("\\r\\n", "");
    str = str.replaceAll("\\t", "");
    str = str.replaceAll("\\\"", "\"");

    return responseBuilder.entity(str).build();
}

JSON 文件:

"CERTIFICATE_STATUS_PASSWORD_INVALID":"Unable to read certificate with the given password.\nUpload the certificate with a valid password."

【讨论】:

  • 谢谢!稍后会尝试并回复您。
  • 这行得通——我只是认为拦截器可能是处理这个问题的好方法!你怎么看?
  • 他们说要保留换行符,但看起来你要把它们去掉?...
猜你喜欢
  • 2011-12-29
  • 2013-07-27
  • 2013-11-10
  • 2015-08-16
  • 2012-11-24
  • 1970-01-01
  • 1970-01-01
  • 2017-05-17
相关资源
最近更新 更多