【问题标题】:How can I add items to a JSON Object properly?如何正确地将项目添加到 JSON 对象?
【发布时间】:2020-08-30 04:06:30
【问题描述】:

1) 下面附上更新的代码已根据我的需要更改了文件名:大多数错误在于TestObjectToJson方法。输出:想要返回json对象

''' }'''

2) 调用这个json输出的另一种方法,只返回接收到的id 输出:134

【问题讨论】:

  • 什么错误? JSONObject 来自哪个包?
  • 您是在问如何创建legs?只是put 另一个JSONObject
  • 公共类 TestObjectToJson { private Long tradeid = 145L;私人多头 trade_Group_id = 324L;私人字符串目标=“LEX”;私人无效主(字符串[] args){TestObjectToJson j =新TestObjectToJson(); obj.put("target_group_id", "324L"); obj.put("目标", "LEX"); obj.put("trade_id", "145L"); obj.put("状态", "合格"); Gson gson = 新 Gson();字符串 json = gson.toJson(obj); System.out.println(json); }
  • 你为什么使用 GSON 和 JSONObject?选择一个图书馆。另外,不要在 cmets 中发布代码。请阅读How do I ask a good question?
  • 我没有看到任何错误。真的不清楚你在问什么。

标签: java json


【解决方案1】:

终极更新

// Root.java
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import java.util.List;

@JsonPropertyOrder({"received_id", "target", "legs"})
public class Root {
    private long receivedId;
    private String target;
    private List<Leg> legs;

    public long getReceivedId() { return receivedId; }
    public void setReceivedId(long id) { this.receivedId = id; }

    public String getTarget() { return target; }
    public void setTarget(String t) { this.target = t; }

    public List<Leg> getLegs() { return legs; }
    public void setLegs(List<Leg> legs) { this.legs = legs; }
}

// Leg.java

public class Leg {
    private long receivedGroupId;
    private String status;

    public long getReceivedGroupId() { return receivedGroupId; }
    public void setReceivedGroupId(long id) { this.receivedGroupId = id; }

    public String getStatus() { return status; }
    public void setStatus(String st) { this.status = st; }
}

// Test.java
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;

import java.util.Arrays;

public class Test {
    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper()
                .setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);

        Root root = new Root();
        root.setReceivedId(134L);
        root.setTarget("none");
        Leg leg = new Leg();
        leg.setReceivedGroupId(234L);
        leg.setStatus("active");
        root.setLegs(Arrays.asList(leg));
        String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(root);

        System.out.println(json);
    }
}

// output
{
  "received_id" : 134,
  "target" : "none",
  "legs" : [ {
    "received_group_id" : 234,
    "status" : "active"
  } ]
}

【讨论】:

  • private Legs 腿应该是 private List 腿吧?您还认为在根类中使类分支静态是一个好的决定吗?
  • 应该是私有 List 腿吧? 是的。 您还认为在根类中将类分支设为静态是一个好的决定吗? 哪个类leg?我们有类Leg,是的,可以接受。为了简洁起见,有静态类。
  • 是的,是在谈论 Leg 类。谢谢你也请更新你的答案!
  • 有什么用?有什么区别吗?
  • 私有列表 腿; public List getLegs() { 返回腿; } 公共无效 setLegs(List 腿) { this.legs = 腿;我已经添加了这个但是“root.setLegs(List);”抛出错误这是正确的格式吗?
猜你喜欢
  • 1970-01-01
  • 2021-01-29
  • 1970-01-01
  • 1970-01-01
  • 2021-11-14
  • 1970-01-01
  • 1970-01-01
  • 2018-01-19
  • 1970-01-01
相关资源
最近更新 更多