0

我如何用 GSON 解析这个?因为这个以数字“1”和“2”开头。我已经为以 entity_id 开头的对象创建了一个对象,并且当我删除了外部 json(其中包含“1”)时它正在工作。

但我想解析这一大堆代码。那么我应该怎么做来解析“1”和“2”呢?像 String id 和 List innerobjects 之类的东西?

{
  "1": {
    "entity_id": "1",
    "status": "complete",
    "coupon_code": null,
    "shipping_description": "Flat Rate - Fixed",
    "customer_id": null,
    "base_discount_amount": "0.0000",
    "base_grand_total": "3422.3800",
    "base_shipping_amount": "90.0000",
    "base_shipping_tax_amount": "0.0000",
    "base_subtotal": "3332.3800",
    "base_tax_amount": "0.0000",
    "base_total_paid": "3422.3800",
    "base_total_refunded": null,
    "discount_amount": "0.0000",
    "grand_total": "3422.3700",
    "shipping_amount": "90.0000",
    "shipping_tax_amount": "0.0000",
    "store_to_order_rate": "1.0000",
    "subtotal": "3332.3700",
    "tax_amount": "0.0000",
    "total_paid": "3422.3700",
    "total_refunded": null,
    "base_shipping_discount_amount": "0.0000",
    "base_subtotal_incl_tax": "3332.3800",
    "base_total_due": "0.0000",
    "shipping_discount_amount": "0.0000",
    "subtotal_incl_tax": "3332.3700",
    "total_due": "0.0000",
    "increment_id": "100000001",
    "base_currency_code": "EUR",
    "discount_description": null,
    "remote_ip": "472.15.1.83",
    "store_currency_code": "EUR",
    "store_name": "Main Website\nMain Website Store\nDefault Store View",
    "created_at": "2012-08-10 09:37:17",
    "shipping_incl_tax": "90.0000",
    "payment_method": "checkmo",
    "gift_message_from": null,
    "gift_message_to": null,
    "gift_message_body": null,
    "tax_name": null,
    "tax_rate": null,
  },
  "2": {
    "entity_id": "2",
    "status": "pending",
    "coupon_code": null,
    "shipping_description": "Flat Rate - Fixed",
    "customer_id": null,
    "base_discount_amount": "0.0000",
    "base_grand_total": "1140.7900",
    "base_shipping_amount": "30.0000",
    "base_shipping_tax_amount": "0.0000",
    "base_subtotal": "1110.7900",
    "base_tax_amount": "0.0000",
    "base_total_paid": null,
    "base_total_refunded": null,
    "discount_amount": "0.0000",
    "grand_total": "1140.7900",
    "shipping_amount": "30.0000",
    "shipping_tax_amount": "0.0000",
    "store_to_order_rate": "1.0000",
    "subtotal": "1110.7900",
    "tax_amount": "0.0000",
    "total_paid": null,
    "total_refunded": null,
    "base_shipping_discount_amount": "0.0000",
    "base_subtotal_incl_tax": "1110.7900",
    "base_total_due": null,
    "shipping_discount_amount": "0.0000",
    "subtotal_incl_tax": "1110.7900",
    "total_due": null,
    "increment_id": "100000002",
    "base_currency_code": "EUR",
    "discount_description": null,
    "remote_ip": "182.11.7.52",
    "store_currency_code": "EUR",
    "store_name": "Main Website\nMain Website Store\nDefault Store View",
    "created_at": "2012-08-15 15:19:35",
    "shipping_incl_tax": "30.0000",
    "payment_method": "checkmo",
    "gift_message_from": null,
    "gift_message_to": null,
    "gift_message_body": null,
    "tax_name": null,
    "tax_rate": null,
  }
}
4

2 回答 2

1

Map对奇数"1", "2", ...对象使用 a :

Map<String, Order> map = gson.fromJson(json, Map.class);
于 2013-02-05T09:54:10.967 回答
0

我找到了另一个解决方案。

GsonBuilder builder = new GsonBuilder();
builder.setFieldNamingStrategy( new FieldNamingStrategy() {
   @Override
   public String translateName( Field field ) {
       if( field.getName().equals( "myNewVariableName" ) ) {
          return "1";
       } else {
          return field.getName();
       }
}
} );
Gson gson = builder.create();
MyCustomClass queryResult = gson.fromJson( json, MyCustomClass.class );

现在写下我在示例“MyCustomClass”中调用的自定义 gson 类。添加一个名为“myNewVariableName”的字段。如果 Gson 序列化或反序列化 json 或您的自定义类,它使用 FieldNamingStrategy 并将您的字段“myNewVariableName”替换为“1”。通过这种方式,您可以减少自己的完整解析,并为您重用 gson 的强大功能,例如“entity_id”等格式正确的字段名称。

于 2014-03-17T08:33:05.757 回答