简体   繁体   English

如何将对象添加到 JSONArray

[英]How to add object to JSONArray

I want to add an object to an array.我想将一个对象添加到数组中。 If the data of other_amount is more than zero I want to add one object more.如果other_amount的数据大于零,我想再添加一个对象。 If it's equal to zero, it should add nothing.如果它等于零,它应该什么都不加。 This is my code:这是我的代码:

JSONArray acc_data = new JSONArray();
Map<String, Object> myaccount = new LinkedHashMap<>();
for (int i = 0; i < mpay.size(); i++) {
    if(other_amount>0){
        myaccount.put("poAccount", other_account);
        myaccount.put("poAmount", other_amount);
        system.out.println(myaccount);
        //{poAccount=050017, poAmount=12}
    }

    myaccount.put("poAccount", amount_account);
    myaccount.put("poAmount", amount);
    system.out.println(myaccount);
    //{"poAccount":"050016","poAmount":"800"}

    acc_data.add(myaccount);
    system.out.println(acc_data);
    //[{"poAccount":"050016","poAmount":"800"}]
}

But I need it like this:但我需要这样:

//[{"poAccount":"050016","poAmount":"800"},{poAccount=050017, poAmount=12}]

please help me to resolve it.请帮我解决它。

You shouldn't use map for your case.您不应该在您的情况下使用地图。 When your put the pair with existing in map key, the pair will be overwrited.当您将现有的对放在映射键中时,该对将被覆盖。 For example例如

map.put ("k1","v1");

Map contains one pair "k1":"v1" The next call映射包含一对 "k1":"v1" 下一个调用

map.put ("k1","newV1");

The first pair will be overwrited and map still contains 1 pair: "k1":"newV1"第一对将被覆盖,地图仍然包含一对:“k1”:“newV1”

For your case it's better to define simple POJO class with 2 fields poAccount and poAmount .对于您的情况,最好使用 2 个字段poAccountpoAmount定义简单的 POJO 类。 And add them to the JSONArray并将它们添加到 JSONArray

The approach you are following, it will not serve your requirement.您遵循的方法将无法满足您的要求。 You should use pojo to store the records and then populate the Json Array.您应该使用 pojo 来存储记录,然后填充 Json 数组。 You can have a look at this code and modify as per your requirements.您可以查看此代码并根据您的要求进行修改。

public class Test {

public static void main(String[] args) {

    Mypojo mypojo = new Mypojo();
    Gson gson = new Gson();
    JSONArray records = new JSONArray();
    for (int i = 0; i < 1; i++) {
        if (5 > 0) {
            mypojo.setPoAccount("050017");
            mypojo.setPoAmount("12");
            JSONObject objects = new JSONObject(gson.toJson(mypojo));
            records.put(objects);
        }

        mypojo.setPoAccount("050016");
        mypojo.setPoAmount("800");
        JSONObject objects = new JSONObject(gson.toJson(mypojo));
        records.put(objects);
    }

    System.out.println(records);

}

}

Mypojo Class : Mypojo类:

public class Mypojo
{
private String poAmount;

private String poAccount;

public String getPoAmount ()
{
    return poAmount;
}

public void setPoAmount (String poAmount)
{
    this.poAmount = poAmount;
}

public String getPoAccount ()
{
    return poAccount;
}

public void setPoAccount (String poAccount)
{
    this.poAccount = poAccount;
}

@Override
public String toString()
{
    return "ClassPojo [poAmount = "+poAmount+", poAccount = "+poAccount+"]";
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM