如何从JSON创建新的JSON?

时间:2015-11-20 06:57:30

标签: json amazon-dynamodb

我有一个由以下数据组成的JSON:

{
  "comment": {
    "S": "Nice"
  },
  "id": {
    "S": "1ca38300-8938-11e5-8656-9bf2d3249757"
  },
  "postId": {
    "S": "083c1f50-8b84-11e5-9021-7da869825160"
  },
  "spam": {
    "N": "0"
  },
  "tags": {
    "L": [
      {
        "S": "test1"
      },
      {
        "S": "test2"
      }
    ]
  }
}

现在,我想将上面的JSON格式化为以下格式,例如:

{  
 "comment":"Nice",
 "id":"1ca38300-8938-11e5-8656-9bf2d3249757",
 "postId":"083c1f50-8b84-11e5-9021-7da869825160",
 "tags":["test1","test2"]
}

2 个答案:

答案 0 :(得分:0)

加载你的json并像普通对象一样操作,然后保存它。

以下是如何转换对象:

var newObj = {
    comment: oldObj.comment.S,
    id: oldObj.id.S,
    postId: oldObj.postId.S,
    tags: oldObj.tags.L.map(function (entry) {
        return entry.S;
    })
}

答案 1 :(得分:0)

嘿,找到以下代码

假设你上面的jsonObject完全是名字并放在结果对象下。

JSONObject obj = new JSONObject();
obj.put("comment",result.getJSONObject("comment").getString("s"));
obj.put("id",result.getJSONObject("id").getString("s"));
obj.put("postId",result.getJSONObject("postId").getString("s"));



List<String> test = new ArrayList<String>;
JSONArray js = result.getJSONObject("tags").getJSONArray("L");
for(I=o;i<js.length();I++){
test.add(js.get(i).getString("s"));
}

obj.put("tags",test);