从API返回的响应实体中的对象数组中获取不正确的JSON

时间:2017-05-03 15:41:06

标签: java json spring

我有一些对象数组如下:

Object[] userObj = new Object[] {"userName: "+user, 
                      "userId: "+id, "userCategory: "+category};

我想从我的对象中获取以下JSON:

  {
    "fault": {
        "userName": "Test User",
        "userObj": [{
            "userId": "1"
        }, {
            "userCategory": "general"
        }]
    }
}

但我得到的是以下内容:

{
    "fault": {
        "userName: Test User",
        "userObj": [{
            "userId: 1"
        }, {
            "userCategory: general"
        }]
    }
}

有没有简单的方法在JSON中用冒号分隔的键值之间放置双引号?

1 个答案:

答案 0 :(得分:0)

这应该有效

Object[] userObj = new Object[2];
        userObj[0] = Collections.singletonMap("userName", user);
        userObj[1] = Collections.singletonMap("userCategory", category);