【问题标题】:Convert JSON String to Pretty Print JSON output using Jackson使用 Jackson 将 JSON 字符串转换为漂亮的打印 JSON 输出
【发布时间】:2013-01-09 01:46:17
【问题描述】:

这是我拥有的 JSON 字符串:

{"attributes":[{"nm":"ACCOUNT","lv":[{"v":{"Id":null,"State":null},"vt":"java.util.Map","cn":1}],"vt":"java.util.Map","status":"SUCCESS","lmd":13585},{"nm":"PROFILE","lv":[{"v":{"Party":null,"Ads":null},"vt":"java.util.Map","cn":2}],"vt":"java.util.Map","status":"SUCCESS","lmd":41962}]}

我需要将上面的 JSON String 转换为 Pretty Print JSON 输出(使用 Jackson),如下所示:

{
    "attributes": [
        {
            "nm": "ACCOUNT",
            "lv": [
                {
                    "v": {
                        "Id": null,
                        "State": null
                    },
                    "vt": "java.util.Map",
                    "cn": 1
                }
            ],
            "vt": "java.util.Map",
            "status": "SUCCESS",
            "lmd": 13585
        },
        {
            "nm": "PROFILE
            "lv": [
                {
                    "v": {
                        "Party": null,
                        "Ads": null
                    },
                    "vt": "java.util.Map",
                    "cn": 2
                }
            ],
            "vt": "java.util.Map",
            "status": "SUCCESS",
            "lmd": 41962
        }
    ]
}

谁能根据我上面的例子给我一个例子?如何实现这个场景?我知道有很多例子,但我无法正确理解这些例子。任何帮助都将通过一个简单的示例表示赞赏。

更新:

下面是我正在使用的代码:

ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.defaultPrettyPrintingWriter().writeValueAsString(jsonString));

但这不适用于我需要上述输出的方式。

这是我用于上述 JSON 的 POJO:

public class UrlInfo implements Serializable {

    private List<Attributes> attribute;

}

class Attributes {

    private String nm;
    private List<ValueList> lv;
    private String vt;
    private String status;
    private String lmd;

}


class ValueList {
    private String vt;
    private String cn;
    private List<String> v;
}

谁能告诉我我是否为 JSON 获得了正确的 POJO?

更新:

String result = restTemplate.getForObject(url.toString(), String.class);

ObjectMapper mapper = new ObjectMapper();
Object json = mapper.readValue(result, Object.class);

String indented = mapper.defaultPrettyPrintingWriter().writeValueAsString(json);

System.out.println(indented);//This print statement show correct way I need

model.addAttribute("response", (indented));

下面一行打印出类似这样的内容:

System.out.println(indented);


{
  "attributes" : [ {
    "nm" : "ACCOUNT",
    "error" : "null SYS00019CancellationException in CoreImpl fetchAttributes\n java.util.concurrent.CancellationException\n\tat java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:231)\n\tat java.util.concurrent.FutureTask.",
    "status" : "ERROR"
  } ]
}

这是我需要展示的方式。但是当我像这样将它添加到模型时:

model.addAttribute("response", (indented));

然后将其显示在如下的结果表单 jsp 页面中:

    <fieldset>
        <legend>Response:</legend>
            <strong>${response}</strong><br />

    </fieldset>

我得到这样的东西:

{ "attributes" : [ { "nm" : "ACCOUNT", "error" : "null    
SYS00019CancellationException in CoreImpl fetchAttributes\n 
java.util.concurrent.CancellationException\n\tat 
java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:231)\n\tat 
java.util.concurrent.FutureTask.", "status" : "ERROR" } ] }

我不需要。我需要上面打印出来的方式。谁能告诉我为什么会这样?

【问题讨论】:

    标签: java json jackson pretty-print


    【解决方案1】:

    要缩进任何旧的 JSON,只需将其绑定为 Object,例如:

    Object json = mapper.readValue(input, Object.class);
    

    然后用缩进写出来:

    String indented = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
    

    这避免了您必须定义实际的 POJO 来将数据映射到。

    或者您也可以使用JsonNode(JSON 树)。

    【讨论】:

    • 谢谢 StaxMan,我想这行得通。当我打印缩进时,我得到了我需要的方式。但是当我使用缩进添加到模型中以便我可以在结果表单页面中显示它们时。它仍然以两三行打印。我已经更新了这个问题,也许你会对现在发生的事情有更多的了解。
    • 问题出在 Spring 上——我猜它需要一个 POJO 作为属性,而不是一个预先格式化的字符串。因此,与其尝试自己格式化,不如告诉 Spring 这样做。使用 Jackson 时,应该可以将其配置为使用缩进。虽然老实说,我不知道为什么你甚至需要缩进它来响应。
    • 嗨,defaultPrettyPrintingWriter() 已被弃用。从 1.9 开始,改用 writerWithDefaultPrettyPrinter()。参考:jackson.codehaus.org/1.9.0/javadoc/org/codehaus/jackson/map/…
    • 对于 Jackson 2,使用 SerializationFeature.INDENT_OUTPUT,如下面的 Marcelo C. 所指定
    • 知道如何使用杰克逊发布书面价值的数量吗?
    【解决方案2】:

    最简单也是最紧凑的解决方案(针对v2.3.3):

    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    mapper.writeValueAsString(obj)
    

    【讨论】:

    • 您实际上可以进一步缩短它:ObjectMapper mapper = new ObjectMapper.enable(SerializationFeature.INDENT_OUTPUT);
    【解决方案3】:

    使用 Jackson 1.9+ 的新方法如下:

    Object json = OBJECT_MAPPER.readValue(diffResponseJson, Object.class);
    String indented = OBJECT_MAPPER.writerWithDefaultPrettyPrinter()
                                   .writeValueAsString(json);
    

    输出将被正确格式化!

    【讨论】:

    • 不幸的是,如果我的输入是运行时创建的对象,而不是另一个 json,这将无济于事。
    • @Innokenty 然后跳过第一行。
    • @muttonUp 是的,很明显。我什至做到了,我不知道我为什么留下这么愚蠢的评论=)
    【解决方案4】:

    对于 Jackson 1.9,我们可以使用以下代码进行漂亮的打印。

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.enable(SerializationConfig.Feature.INDENT_OUTPUT);
    

    【讨论】:

      【解决方案5】:

      我认为,这是美化json数据最简单的技术,

      String indented = (new JSONObject(Response)).toString(4);
      

      其中 Response 是一个字符串。

      只需在 toString() 方法中传递 4(indentSpaces)。

      注意:它可以在没有任何库的android中正常工作。但是在java中你必须使用org.json库。

      【讨论】:

      • 值得注意的是,这是使用 JSON in Java (org.json) 库。
      • 在android中,无需任何libraray即可直接使用。
      • String json = new GsonBuilder().setPrettyPrinting().create().toJson(map); String indentedJson = (new JSONObject(json)).toString(4); 由于某种原因,第二个失去了键的顺序
      • 不幸的是,当前的方法不能处理 json 对象列表。我的意思是 [{"id":"1"}, {"id":"2"}]
      【解决方案6】:

      ObjectMapper.readTree() 可以一行完成:

      mapper.readTree(json).toPrettyString();
      

      由于readTree 产生一个JsonNode,这几乎总是会产生等效的漂亮格式的JSON,因为它JsonNode 是底层JSON 字符串的直接树表示。

      Jackson 2.10 之前的版本

      JsonNode.toPrettyString() 方法是在 Jackson 2.10 中添加的。在此之前,需要第二次调用 ObjectMapper 来编写格式化的结果:

      mapper.writerWithDefaultPrettyPrinter()
              .writeValueAsString(mapper.readTree(json));
      

      【讨论】:

      • 我喜欢这个答案的原因是,除了直接映射到 JSON 类型之外,它根本不进行任何对象转换。只要输入字符串是有效的 JSON,我们就知道输出字符串在语义上是等效的 JSON。
      • 这应该是公认的答案,因为正如@M.Justin 已经提到的,我们不进行任何对象转换,如果 JSON 可以是任何东西,这是必须的。当您可以像此答案中提出的那样轻松时,为什么要像其他答案中提出的那样使生活变得艰难...
      • @Akito 我只是进一步简化了答案,这要归功于在 Jackson 2.10 中添加了toPrettyString()
      【解决方案7】:

      您可以使用以下方式实现此目的:

      1.使用杰克逊

          String formattedData=new ObjectMapper().writerWithDefaultPrettyPrinter()
      .writeValueAsString(YOUR_JSON_OBJECT);
      

      导入波纹管类:

      import com.fasterxml.jackson.databind.ObjectMapper;
      

      它的gradle依赖是:

      compile 'com.fasterxml.jackson.core:jackson-core:2.7.3'
      compile 'com.fasterxml.jackson.core:jackson-annotations:2.7.3'
      compile 'com.fasterxml.jackson.core:jackson-databind:2.7.3'
      

      2。使用 Google 的 Gson

      String formattedData=new GsonBuilder().setPrettyPrinting()
          .create().toJson(YOUR_OBJECT);
      

      导入波纹管类:

      import com.google.gson.Gson;
      

      它的gradle是:

      compile 'com.google.code.gson:gson:2.8.2'
      

      在这里,您还可以从存储库中下载正确的更新版本。

      【讨论】:

      • Jackson 不是由 Apache 开发的。它只使用 Apache 许可证。
      • @Gili,感谢您的指正。我将更改文本。
      【解决方案8】:

      This looks like it might be the answer to your question。它说它正在使用 Spring,但我认为这对你的情况仍然有帮助。让我在这里内联代码,这样更方便:

      import java.io.FileReader;
      
      import org.codehaus.jackson.map.ObjectMapper;
      import org.codehaus.jackson.map.ObjectWriter;
      
      public class Foo
      {
        public static void main(String[] args) throws Exception
        {
          ObjectMapper mapper = new ObjectMapper();
          MyClass myObject = mapper.readValue(new FileReader("input.json"), MyClass.class);
          // this is Jackson 1.x API only: 
          ObjectWriter writer = mapper.defaultPrettyPrintingWriter();
          // ***IMPORTANT!!!*** for Jackson 2.x use the line below instead of the one above: 
          // ObjectWriter writer = mapper.writer().withDefaultPrettyPrinter();
          System.out.println(writer.writeValueAsString(myObject));
        }
      }
      
      class MyClass
      {
        String one;
        String[] two;
        MyOtherClass three;
      
        public String getOne() {return one;}
        void setOne(String one) {this.one = one;}
        public String[] getTwo() {return two;}
        void setTwo(String[] two) {this.two = two;}
        public MyOtherClass getThree() {return three;}
        void setThree(MyOtherClass three) {this.three = three;}
      }
      
      class MyOtherClass
      {
        String four;
        String[] five;
      
        public String getFour() {return four;}
        void setFour(String four) {this.four = four;}
        public String[] getFive() {return five;}
        void setFive(String[] five) {this.five = five;}
      }
      

      【讨论】:

      • 感谢丹尼尔的帮助。我遇到的最困难的部分是如何将我的 JSON 建模为一个类?如果我让这部分工作,我可以轻松编写其余部分。
      • 你能看看我用 JSON 写的 POJO 类吗?看起来对不对?
      【解决方案9】:

      由于jackson-databind:2.10JsonNode 有toPrettyString() 方法可以轻松格式化JSON:

      objectMapper
        .readTree("{}")
        .toPrettyString()
      ;
      

      来自the docs

      public @987654323@ toPrettyString()

      替代toString() 使用 Jackson 默认的漂亮打印机序列化此节点。

      自:
      2.10

      【讨论】:

        【解决方案10】:

        如果您格式化字符串并返回 RestApiResponse&lt;String&gt; 之类的对象,您将得到不需要的字符,例如转义等:\n\"。解决方案是将您的 JSON 字符串转换为 Jackson JsonNode 对象并返回 RestApiResponse&lt;JsonNode&gt;:

        ObjectMapper mapper = new ObjectMapper();
        JsonNode tree = objectMapper.readTree(jsonString);
        RestApiResponse<JsonNode> response = new RestApiResponse<>();
        apiResponse.setData(tree);
        return response;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-01-31
          • 1970-01-01
          • 1970-01-01
          • 2014-03-18
          • 2013-07-07
          • 1970-01-01
          • 2013-07-11
          相关资源
          最近更新 更多