【问题标题】:how to create multiple list array如何创建多个列表数组
【发布时间】:2018-08-24 03:12:21
【问题描述】:

我从查询中得到 Alphabet 并列出 Country,我怎样才能制作这样的 JSON? 我不知道这个。我怎样才能创建一些这样的 JSON?

> List=[
>     {Alphabet:'B',
>     Country:[
>       {name: 'Belgia'}
>     ]},
>     {Alphabet:'I',
>     Country:[
>       {name: 'Indonesia'}
>     ]},
>     {Alphabet:'N',
>     Country:[
>       {name: 'Namibia'}
>     ]},
>     {Alphabet:'O',
>     Country:[
>       {name: 'Ohio'}
>     ]},
>     {Alphabet:'T',
>     Country:[
>       {name: 'Turki'},
>       {name: 'Tunisia'}
>     ]},
>     {Alphabet:'Y',
>     Country:[
>       {name: 'Yaman'},
>     ]}    ];

我有一些这样的模型

> public class ListCountry {
>     private String Alphabet;
>     List<ListCountryObj> List;
>     
>     public void setAlphabet(String Alphabet){this.Alphabet= Alphabet;}
>     public void setList(List<ListCountryObj> List){this.List  = List;}
>     
>     public String getAlphabet(){return this.Alphabet;}
>     public List<ListCountryObj> getList() {return List;} }

还有这个

> public class ListCountryObj {
>     private String country;
>     
>     public void setCountry(String country){this.country = country;}
>     public String getCountry(){return this.country;} }

【问题讨论】:

    标签: arrays json api


    【解决方案1】:
    1. 创建一个空数组
    2. 映射结果
    3. 获取位置 0 的字符
    4. 使用 Alphabet == 该字符检查数组中的对象
    5. 如果不存在,则创建对象 {Alphabet: A, countries [“America”]}
    6. 如果确实存在,只需将国家/地区推到国家/地区数组中即可。

    【讨论】:

    • 对于第 5 点,我认为如果我在数据库中添加一些数据,它应该会自动添加。抱歉,我在我的问题中添加了更多信息
    【解决方案2】:
    List<Map<String, List<String>
    

    通过扩展 Map 创建两个模型,其中一个具有 Map 对象。其他是该对象的列表。

    Class A extends map { // add all the override methods
    
     // annotate with name you want in json
      String key;
      // annotate with name you want in json
      List<String> value
      }
    
      Class B {
        List<A> listOfA;
       }
    

    确保构建对象 A 和对象 B。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-15
      • 2011-12-14
      • 1970-01-01
      • 2014-01-24
      • 2016-03-03
      • 2020-03-18
      • 2017-03-27
      相关资源
      最近更新 更多