【问题标题】:JsonArray output is not workingJsonArray 输出不起作用
【发布时间】:2016-02-16 13:32:46
【问题描述】:

我对 json 很陌生,所以我不明白 im doing wrong.. I want my data json output like this, but im 做得不好。

{ "data": [
    ["2014-01", 71173],
    ["2014-02", 57624],
    ["2014-03", 64851],
    ["2014-04", 60486],
    ["2014-05", 60500],
    ["2014-06", 62908],
    ["2014-07", 64818],
    ["2014-08", 59961],
    ["2014-09", 58542],
    ["2014-10", 22050]
  ]  }

这是我得到的:

{ "data": [
"hallo 0",
"hallo 10",
"hallo 20",
"hallo 30",
"hallo 40",
"hallo 50",
"hallo 60",
"hallo 70",
"hallo 80",
"hallo 90" ] }

这是名称为 TestTest 的数据的类

public class TestTest {

    @JsonProperty("data")
    private List<List<Object>> data = new ArrayList<List<Object>>();

    public TestTest(){

    }

    @JsonProperty("data")
    public List<List<Object>> getData() {
        return data;
    }

    @JsonProperty("data")
    public void setData(List<List<Object>> data) {
        this.data = data;
    }

}


    @GET
    @Path("/CallsPerMinuteAsLineChart")
    public Response getTest(){
        TestTest test = new TestTest();
        List<List<Object>> data = new ArrayList<List<Object>>();

        int loop;

        for(loop=0; loop < 100; loop = loop + 10){
            List<Object> dataitem = new ArrayList<>();
            dataitem.add("hallo");
            dataitem.add(loop);
            data.add(dataitem);
        }

        test.setData(data);
        return Response.ok(test).build();
    }

【问题讨论】:

    标签: java json api jersey jackson


    【解决方案1】:

    问题不在于您的 json 结构,尽管创建这样的嵌套列表不是一个好习惯,但我尝试使用 GsonJackson 编写您的代码

            TestTest test = new TestTest();
            List<List<Object>> data = new ArrayList<List<Object>>();
    
            int loop;
    
            for( loop = 0; loop < 100; loop = loop + 10 ){
                List<Object> dataitem = new ArrayList<>();
                dataitem.add( "hallo" );
                dataitem.add( loop );
                data.add( dataitem );
            }
    
            test.setData( data );
    
            ObjectMapper mapper = new ObjectMapper();
            String jackson = mapper.writeValueAsString( test );
    
            System.out.println( new Gson().toJson( test ) );
            System.out.println( jackson );
    

    此代码打印:

    {"data":[["hallo",0],["hallo",10],["hallo",20],["hallo",30],["hallo",40],["hallo",50],["hallo",60],["hallo",70],["hallo",80],["hallo",90]]}
    {"data":[["hallo",0],["hallo",10],["hallo",20],["hallo",30],["hallo",40],["hallo",50],["hallo",60],["hallo",70],["hallo",80],["hallo",90]]}
    

    这正是您想要的。由于没有人在不知道所有细节的情况下明确解决您的问题,我建议您这样使用它:

    @GET
    @Path("/CallsPerMinuteAsLineChart")
    public String getTest(){
    

    在这个方法中,我只返回我转换它们的 json 字符串。

    【讨论】:

      【解决方案2】:
       List<Object> dataitem = new ArrayList<>();
              dataitem.add( "hallo" );
              dataitem.add( loop );
              data.add( dataitem );
      

      在上面的代码中,您在循环中添加了一个对象(数据项),这就是您在输出中看到的内容。我建议,而不是在列表中使用对象(字符串键,字符串 val)类型或映射(k,v) - 可能的选项

      1. User Map
            Map<String, Object> dataitem = new HashMap<String, Object>();
      Iterate the map and print object properties.
      2. List of Object - where Object can have id and value as attribute that you print.
            List<Object> dataitem = new ArrayList<Object>();
      or Simply 
       3. Map<String, String> dataitem = new HashMap<String, String>();
      Pring k.V for map
      

      【讨论】:

        【解决方案3】:

        解决这个问题的一个好方法是创建可序列化的对象,例如

        public class Item {
        
            private String label;
            private int value;
        
            public Item() {
        
            }
        
            public Item(String label, int number) {
                this.label = label;
                this.number = number;
            }
        
            public void setLabel(String label) {
                this.label = label;
            }
        
            public int getLabel() {
                return label;
            }
        
            public void setNumber(int number) {
                this.number = number;
            }
        
            public int getNumber() {
                return number;
            }
        }
        
        public class Data {
        
            private List<Item> items;
        
            public Data() {
                items = new ArrayList<>();
            }
        
            public void setItems(List<Item> items) {
                this.items = items;
            }
        
            public List<Item> getItems() {
                return items;
            }
        
            public void addItem(Item item) {
                items.add(item);
            }
        }
        

        你的方法:

        @GET
        @Path("/callsperminuteaslinechart")
        public Response getTest(){
            Data data = new Data();
        
            for (int i = 10; i <= 100; i += 10) {
                data.addItem(new Item("Hello", i));
            }
        
            return Response.ok(data).build();
        }
        

        【讨论】:

          【解决方案4】:

          分享我所做的,我认为这与其他人在我之前分享的内容一致。

          package general;
          
          import java.util.ArrayList;
          import java.util.List;
          
          import org.json.JSONObject;
          
          public class GenerateJSONout {
          
          public static void main(String[] args) {
          
              List<List<Object>> data = new ArrayList<List<Object>>();
          
              int loop;
          
              for( loop = 0; loop < 100; loop = loop + 10 ){
                  List<Object> dataitem = new ArrayList<>();
                  dataitem.add( "hallo" );
                  dataitem.add( loop );
                  data.add( dataitem );
              }
          
          
              JSONObject jo = new JSONObject();       
              jo.put("data", data);
          
              System.out.println(jo.toString());
          }
          

          } 结果:

          {"data":[["hallo",0],["hallo",10],["hallo",20],["hallo",30],["hallo",40],["hallo",50],["hallo",60],["hallo",70],["hallo",80],["hallo",90]]}
          

          【讨论】:

          • 这不是这个人想要的答案。
          猜你喜欢
          • 2018-09-22
          • 2020-01-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-02-18
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多