我如何用GSON解析这个?因为这个以数字#34; 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,
}
}
答案 0 :(得分:1)
将Map
用于奇数"1", "2", ...
个对象:
Map<String, Order> map = gson.fromJson(json, Map.class);
答案 1 :(得分: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 );
立即写下您在示例&#39; MyCustomClass&#39;中调用的自定义gson类。 添加名称为&#39; myNewVariableName&#39;的字段。如果Gson序列化或反序列化json或您的自定义类,则使用FieldNamingStrategy并替换您的字段&#39; myNewVariableName&#39;用&#39; 1&#39;。 通过这种方式,您可以减少自己的完整解析,并为您构建良好的字段名称重用gson的功能,例如&lt; entity_id&#39;。