将具有多个对象的JSON String转换为类实例

时间:2016-11-04 02:40:17

标签: java json jackson

如何将包含多个对象的Json String转换为java对象:

编辑:它似乎不太可行,但这会创建类语句的2个类实例:

String JsonStr = "{\"bills\":[{\"amount\":\"13\",\"billId\":\"billid3\"}] ,\"bills\":[{\"amount\":\"155\",\"billId\":\"billid4\"}]}";
ObjectMapper mapper = new ObjectMapper();
Statement obj = mapper.readValue(JsonStr, obj.class);

如果没有,我如何为接收带有多个相同类型的唯一对象(银行对帐单)的JSON字符串的程序编写代码,并为其创建适当数量的java对象实例? 我非常感谢任何帮助。

非常感谢!

2 个答案:

答案 0 :(得分:1)

我使用过gson-2.2.2.jar。

请找到以下代码:

<强> Bill.java

public class Bill
{
    private double  billAmount;

    private String  billId;

    //getters and setters
}

<强> Main.java

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class Main
{
    public static void main(String[] args)
    {
        Bill bill = null;
        List<Bill> bills = new ArrayList<Bill>();
        for (int i = 0; i < 5; i++)
        {
            bill = new Bill();
            bill.setBillAmount(100 + (i + 1));
            bill.setBillId("bill_id_" + (i + 1));
            bills.add(bill);
        }
        Gson gson = new Gson();
        String json = gson.toJson(bills, new TypeToken<List<Bill>>()
        {}.getType());
        System.out.println(json);

        Type mapType = new TypeToken<List<Bill>>()
        {}.getType();
        List<Bill> billsRetrieved = new Gson().fromJson(json, mapType);
        for (Bill bill2 : billsRetrieved)
        {
            System.out.println(bill2.getBillId());
        }
    }
}

OutPut:

[
   {
      "billAmount":101.0,
      "billId":"bill_id_1"
   },
   {
      "billAmount":102.0,
      "billId":"bill_id_2"
   },
   {
      "billAmount":103.0,
      "billId":"bill_id_3"
   },
   {
      "billAmount":104.0,
      "billId":"bill_id_4"
   },
   {
      "billAmount":105.0,
      "billId":"bill_id_5"
   }
]

bill_id_1 bill_id_2 bill_id_3 bill_id_4 bill_id_5

如果您需要进一步说明,请退回。

答案 1 :(得分:1)

我认为你提供的json字符串应该像

  

&#34; {\&#34;票据\&#34;:[{\&#34;量\&#34;:\&#34; 13 \&#34; \&#34 ; BILLID \&#34;:\&#34; billid3 \&#34;}   {\&#34;量\&#34;:\&#34; 155 \&#34; \&#34; BILLID \&#34;:\&#34; billid4 \&#34;} ]}&#34;

如果是这种情况,您可以使用以下解决方案:

创建两个类 Bill.java TestObject.java ,如下所示:

<强> Bill.java

public class Bill {
    private double amount;
    private String billId;
    /**
     * @return the amount
     */
    public double getAmount() {
        return amount;
    }
    /**
     * @param amount the amount to set
     */
    public void setAmount(double amount) {
        this.amount = amount;
    }
    /**
     * @return the billId
     */
    public String getBillId() {
        return billId;
    }
    /**
     * @param billId the billId to set
     */
    public void setBillId(String billId) {
        this.billId = billId;
    }

}

<强> TestObject.java

import java.util.List;

public class TestObject {

    private List<Bill> bills;

    /**
     * @return the bills
     */
    public List<Bill> getBills() {
        return bills;
    }

    /**
     * @param bills the bills to set
     */
    public void setBills(List<Bill> bills) {
        this.bills = bills;
    }

}

这是测试代码的主程序。

<强> Test.java

import com.fasterxml.jackson.databind.ObjectMapper;

public class Test {

    public static void main(String[] args) {
        String jsonStr = "{\"bills\":[{\"amount\":\"13\",\"billId\":\"billid3\"} ,{\"amount\":\"155\",\"billId\":\"billid4\"}]}";

        ObjectMapper mapper = new ObjectMapper();
        try {
            TestObject testObject = mapper.readValue(jsonStr, TestObject.class);
            System.out.print(testObject);
        } catch (Exception e) {
            e.printStackTrace();
        } 
    }
}