English 中文(简体)
兑换 纽约总部 甲型六氯环己烷
原标题:convert JSON Type to Byte array format in java

我在希望利用《国家发展计划议定书》中的逐个格式发送数据时,问题在于我试图用 j子物体生成数据时,我无法得到我的数据的逐个格式,这是我的样本。

    JSONObject obj = new JSONObject();
    obj.put("name", "foo");
    obj.put("num", new Integer(100));
    obj.put("balance", new Double(1000.21));
    obj.put("is_vip", new Boolean(true));
    obj.put("nickname",null);

    sendData = obj.getBytes(); //this is error because not have methos getBytes();

i) 我知道我的问题,但我可以发现如何将json的反对改为 by,任何建议?

最佳回答

引证:

obj.toString().getBytes(theCharset);
问题回答

假设JSONObject,请见,即

sendData = obj.toString().getBytes("utf-8");

为避免从<代码>String到>逐[>执行根据规定charset制定的编码的不必要转换,我宁愿直接使用<代码>Byte ArrayOutputStream。

ByteArrayOutputStream stream = new ByteArrayOutputStream();
Json.createWriter(stream).write(obj);

byte[] sendData = stream.toByteArray()

System.out.println("Bytes array: " + sendData);
System.out.println("As a string: " + stream.toString());

此外,还可以提供前印<>:>:

Json.createWriterFactory(
            Collections.singletonMap(JsonGenerator.PRETTY_PRINTING, true))
        .createWriter(stream)
        .write(obj);

唯一可悲的是,它不是one-liner。 你们至少需要3份(因为你 o了<条码>,JsonWriter.close(,在这方面没有必要)。

用户类别:ObjectMapper>>> 代码> jackson-databind Project,ie objectMapper.writeValueAsBytes(dto) 回归代码>byte [ ]

@Autowired
private ObjectMapper objectMapper;

ContractFilterDTO filter = new ContractFilterDTO();
    mockMvc.perform(post("/api/customer/{ico}", "44077866")
            .contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
            .content(objectMapper.writeValueAsBytes(filter)))...

Maven Depend:

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.8.1</version>
</dependency>




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签