alibaba / alibaba/fastjson2

使用 JSON.toJSONString 把 hashmap对象转json字符串时,出现 空字符串key

Open
#1,431 11 comments 0 reactions 0 assignees View on GitHub
question
Dominant language
Java
Stars
4.4k
Forks
613
Avg merge
1d 22h
Merged PRs (30d)
6

Description

### 问题描述
*简要描述您碰到的问题。*
使用 JSON.toJSONString 把 hashmap对象转json字符串时,出现 空字符串key,并且无法正常解析
1.x版本没有出现,只在2.x版本中存在

### 环境信息
*请填写以下信息:*

- OS信息: [e.g.:windows10]
- JDK信息: [e.g.:jdk 1.8.0_161]
- 版本信息:[e.g.:Fastjson2 2.0.29]

### 重现步骤
*如何操作可以重现该问题:*
```
public static void main(String[] args) {
PlanResponseResultTest test = new PlanResponseResultTest();
Map resMap = new HashMap();
List agreements = new ArrayList<>();
TelecomAgreementTest ta = new TelecomAgreementTest();
ta.setAgreement("哈哈");
agreements.add(ta);
test.setAgreements(agreements);
ChannelConfInfoTest cci = new ChannelConfInfoTest();
cci.setIdOldStatus(0);
test.setChannelConf(cci);
resMap.put("agreements", test.getAgreements());
resMap.put("channelConf", test.getChannelConf());
resMap.put("planInfo",test);
System.out.println(JSON.toJSONString(resMap));
}

public class PlanResponseResultTest implements Serializable {
private static final long serialVersionUID = 3227979509354723203L;
private List agreements;
private ChannelConfInfoTest channelConf;
public List getAgreements() {
return agreements;
}
public void setAgreements(List agreements) {
this.agreements = agreements;
}
public ChannelConfInfoTest getChannelConf() {
return channelConf;
}
public void setChannelConf(ChannelConfInfoTest channelConf) {
this.channelConf = channelConf;
}
}

public class ChannelConfInfoTest implements Serializable {
private static final long serialVersionUID = 1L;
private String sellChannel;
private Integer idOldStatus;
public String getSellChannel() {
return sellChannel;
}
public void setSellChannel(String sellChannel) {
this.sellChannel = sellChannel;
}
public Integer getIdOldStatus() {
return idOldStatus;
}
public void setIdOldStatus(Integer idOldStatus) {
this.idOldStatus = idOldStatus;
}
}

public class TelecomAgreementTest implements Serializable {
private static final long serialVersionUID = -3034850306981193452L;
private Long index;
private String url;
private String agreement;
public String getAgreement() {
return agreement;
}
public void setAgreement(String agreement) {
this.agreement = agreement;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Long getIndex() {
return index;
}
public void setIndex(Long index) {
this.index = index;
}
}
```

1. 使用 JSON.toJSONString 方法
2. 输入 hashmap 数据
planInfo 对象就是正常的Java实体类对象
3. 出现 以下 错误
结果:
```
{
"channelConf": {
"idOldStatus": 0
},
"agreements": [{
"agreement": "哈哈"
}],
"planInfo": {
{
"$ref": "$.agreements"
}"channelConf": {
"$ref": "$.channelConf"
}
}
}
```
### 期待的正确结果
*对您期望发生的结果进行清晰简洁的描述。*
```
{
"channelConf": {
"idOldStatus": 0,
"sellChannel": ""
},
"agreements": [{
"agreement": "哈哈",
"index": 0,
"url": ""
}],
"planInfo": {
"channelConf": {
"idOldStatus": 0,
"sellChannel": ""
},
"agreements": [{
"agreement": "哈哈",
"index": 0,
"url": ""
}]
}
}
```

### 相关日志输出
*请复制并粘贴任何相关的日志输出。*

#### 附加信息
*如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。*

Contributor guide

Open the contributing guide

Research direction

Start with the supplied Java main reproducer and the JSON.toJSONString entry point, comparing the reported 1.x and 2.x behavior for the HashMap containing planInfo. Reproduce the malformed output and verify that the result matches the expected nested JSON without the empty key or invalid reference structure.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.