将json数组存储在列表中

时间:2015-01-14 05:35:13

标签: java arrays json

我有一个json,其中包含2个数组,我需要在第1个数据中存储第二个数组的数据。这是我的json

{
    "Classes": [
        {
            "Cls": "1",
            "Section": [
                "a",
                "b"
            ]
        },
        {
            "Cls": "2",
            "Section": [
                "a",
                "b"
            ]
        },
        {
            "Cls": "3",
            "Section": [
                "a",
                "b"
            ]
        },
        {
            "Cls": "4",
            "Section": [
                "a",
                "b"
            ]
        },
        {
            "Cls": "5",
            "Section": [
                "a",
                "b"
            ]
        },
        {
            "Cls": "6",
            "Section": [
                "a",
                "b"
            ]
        },
        {
            "Cls": "7",
            "Section": [
                "a",
                "b"
            ]
        },
        {
            "Cls": "8",
            "Section": [
                "a",
                "b"
            ]
        },
        {
            "Cls": "9",
            "Section": [
                "a",
                "b"
            ]
        },
        {
            "Cls": "10",
            "Section": [
                "a",
                "b"
            ]
        }
    ]
}

我的代码是

int len=json.getJSONArray("Classes").length();

    String[] cls=new String[len];
    System.out.println(len);
    for(int i=0;i<len;i++)
    {   

         cls[i]=(String) json.getJSONArray("Classes").getJSONObject(i).get("Cls").toString();
        System.out.println("Classes="+cls[i]);
        int seclength=json.getJSONArray("Classes").getJSONObject(i).getJSONArray("Section").length();
        System.out.println("Section Length="+seclength);
         for(int j=0;j<seclength;j++)
         {
             Map store=new HashMap();
             String[] section=new String[seclength];
             section[j]=json.getJSONArray("Classes").getJSONObject(i).getJSONArray("Section").get(j).toString();

             store.put("Pclass",cls[i]);
             store.put("Section",section[j]);
             list.add(store);
         }


         }
    System.out.println("List="+list);

目前我的输出是

[{Pclass=1, Section=a}, {Pclass=1, Section=b}, {Pclass=2, Section=a}, {Pclass=2, Section=b}, {Pclass=3, Section=a}, {Pclass=3, Section=b}, {Pclass=4, Section=a}, {Pclass=4, Section=b}, {Pclass=5, Section=a}, {Pclass=5, Section=b}, {Pclass=6, Section=a}, {Pclass=6, Section=b}, {Pclass=7, Section=a}, {Pclass=7, Section=b}, {Pclass=8, Section=a}, {Pclass=8, Section=b}, {Pclass=9, Section=a}, {Pclass=9, Section=b}, {Pclass=10, Section=a}, {Pclass=10, Section=b}]

我需要Pclass 1中的a部分和b部分以及Pclass2中的b部分,以此类推..

3 个答案:

答案 0 :(得分:0)

您需要按如下方式定义POJO:

public class SomeClass {

    private String Cls;
    private List<String> Section = new ArrayList<String>();
    // getters, setters & toString methods
}

以下是将json字符串解析为对应的java Object

的解析逻辑
String jsonString = "YOUR_JSON_STRING";

JSONArray jsonArray = new JSONObject(jsonString).getJSONArray("Classes");

List<SomeClass> list = new ArrayList<SomeClass>();

for (int index = 0; index < jsonArray.length(); index++) {
    JSONObject jsonObject = jsonArray.getJSONObject(index);
    JSONArray sectionArray = jsonObject.getJSONArray("Section");

    SomeClass object = new SomeClass();
    object.setCls(jsonObject.getString("Cls"));

    for (int i = 0; i < sectionArray.length(); i++) {
        object.getSection().add(sectionArray.get(i).toString());
    }

    list.add(object);
}

System.out.println(list);

UPDATE:

如果您想跳过所有解析逻辑,那么可以使用GSON将json字符串解析为Java对象。

为此你需要写另一个班级

public class Main {
    private List<SomeClass> Classes;
    // getters, setters & toString methods
}

然后你的解析代码将是

Gson gson = new GsonBuilder().create();
Main mainObject = gson.fromJson(jsonString, Main.class);
System.out.println(mainObject.getClasses());

使用GSON使用以下maven依赖

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.2.2</version>
</dependency>

<强>输出:

[[Cls=1, Section=[a, b]], [Cls=2, Section=[a, b]], [Cls=3, Section=[a, b]], [Cls=4, Section=[a, b]], [Cls=5, Section=[a, b]], [Cls=6, Section=[a, b]], [Cls=7, Section=[a, b]], [Cls=8, Section=[a, b]], [Cls=9, Section=[a, b]], [Cls=10, Section=[a, b]]]

答案 1 :(得分:0)

Innner for循环: 你:

 for(int j=0;j<seclength;j++)
 {
     Map store=new HashMap();
     String[] section=new String[seclength];
     section[j]=json.getJSONArray("Classes").getJSONObject(i).getJSONArray("Section").get(j).toString();

     store.put("Pclass",cls[i]);
     store.put("Section",section[j]);
     list.add(store);
 }

编辑:

List innerList = new ArrayList();
for(int j=0;j<seclength;j++)
{
    String[] section=new String[seclength];
    section[j]=json.getJSONArray("Classes").getJSONObject(i).getJSONArray("Section").get(j).toString();    
    innerList.add("Section",section[j]);
}
Map store=new HashMap();
store.put("Pclass",innerList);
list.add(store);

答案 2 :(得分:0)

我希望它可以帮助别人 首先我计算了数组长度

JSONArray json=new JSONArray(jsondata);
int len=json.getJSONArray("Classes").length();

然后创建了String数组

String[] classes=new String[len];
for(nt i=0;i<len;i++)
{
 Map map=new Hashmap();
 classes[i]=json.JSONObject(i).get("Cls").toString();
 map.put("cls",Cls[i]);

现在计算内部数组长度

    int innerlen=json.getJSONArray("Classes").getJSONObject(i).getJSONArray("Section").length();
    String[] section=new String[innerlen];
    String secti=null;
for(int j=0;j<innerlen;j++)
    {
 section[j]=json.getJSONArray("Classes").getJSONObject(i).getJSONArray("Section").get(j).length();
    if(secti==null)
    {
      secti=section[j];
    }
     else
    {
     secti=secti+","+section[j];
    map.put("sections",secti);
}
   }
 list.add(map);