在其他 JSONObject 中处理 JSONObject
我想发送 json,里面有另一个 json 对象,像这样
{
"key1": "value1",
"key2": "valu2",
"content": {
"nestedkey1": "nestedValue1",
"nestedkey2": "nestedValue2"
}
}
里面的对象没有任何 java 表示,只有 json 格式的字符串。怎样才能正确转换呢?
我的方法不正确,我总是收到嵌套 json 的空字符串。我对这个嵌套对象使用了Map,但又是空的map。
public class Instance {
private String key1;
private int key2;
private String content;
public String getKey1 {
return key1;
}
public void setKey1(String key1) {
this.key1 = key1;
}
public BigDecimal getKey2() {
return key2;
}
public void setKey2(BigDecimal key2) {
this.key2 = key2;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
I want to send json, which have another json object inside, like this
{
"key1": "value1",
"key2": "valu2",
"content": {
"nestedkey1": "nestedValue1",
"nestedkey2": "nestedValue2"
}
}
Object inside doesn't have any java representation, just string in json-format. How it can be correctly converted?
My approach is not correct, I always receive empty string for nested json. I used Map for this nested object, but empty map again.
public class Instance {
private String key1;
private int key2;
private String content;
public String getKey1 {
return key1;
}
public void setKey1(String key1) {
this.key1 = key1;
}
public BigDecimal getKey2() {
return key2;
}
public void setKey2(BigDecimal key2) {
this.key2 = key2;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JSON 序列化是如何完成的?如果您没有使用 Jackson,那么您应该使用。
Jackson 可以按照您的意愿获取
Map
并将其转换为 JSON,无需任何额外配置。另一方面,如果您使用 Jersey JSON 插件,则必须编写Map
的子类,并向其添加 JAXB 注释 - 有点麻烦。How is the JSON serialization being done? If you're not using Jackson, then you should be.
Jackson can take a
Map
and translate it to JSON just like you want, without any additional configuration. On the other hand, if you're using the Jersey JSON plugin, you'd have to write a subclass ofMap
, and add JAXB annotations to it - kind of a pain in the ass.