我想用gson库创建一个这样的json结构,并创建适当的对象

时间:2017-11-17 06:27:16

标签: android json parsing serialization gson

{
   "restaurantId":"0a60fa5e-642c-4e4f-ae37-a7c8093e1659",
   "restaurantName":"Food Market",
   "paymentRefKey":"cpmqmvpv",
   "paymentStatus":"success",
   "paymentMode":"paypal_account",
   "transactionStatus":"settling",
   "payerId":"PAYER_ID-2f3c6088eb828dab",
   "paypalPaymentId":"PAY-e43a2913d8ad92ccda1cf872",
   "orderNetAmount":"21.825",
   "salesTax":0,
   "serviceFee":1.575,
   "discount":0,
   "subTotal":22.5,
   "orderQuantity":1,
   "userId":"68503c6e-9475-4081-8bdf-199bc194745f",
   "firstName":"test",
   "lastName":"customer",
   "email":"test.customer@yopmail.com",
   "contactNumber":"9516238470",
   "couponcode":{
      "type":"percent",
      "midval":2.25,
      "code":"QDH596"
   },
   "orderLines":[
      {
         "quantity":1,
         "offerId":"2bb21254-fe7d-4695-ae99-d420fdae3aca",
         "startValidity":"2017-09-26T06:55:00.000Z",
         "endValidity":"2017-11-02T01:00:00.000Z",
         "categoryId":"dbae9249-705a-4d7a-86ce-6f5c5c180855",
         "itemName":"Dosa.",
         "menuId":"02bfcaec-0bb1-42b9-87a5-c60e04fcf9fc",
         "actualPrice":25,
         "dealPrice":22.5,
         "netAmount":22.5,
         "discountPercent":10,
         "type":"offer",
         "itemImagePath":"offers/offer_1506408444171.jpeg",
         "unlimitedQuantity":true
      }
   ]
}

这是json我要在gson android pojo生成gson同义词,但我无法生成相同的格式 我使用[ { "contactNumber":"8149425758", "couponcode":{ "midval":0, "type":"percent" }, "discount":0, "email":"udasibharat1@gmail.com", "firstName":"Bharat", "lastName":"Udasi", "orderLines":[ { "actualPrice":50, "dealPrice":45, "discountPercent":0, "endValidity":"1970-01-01T17:30:00.000Z", "itemImagePath":"https://s3.amazonaws.com/bigzpoon-develop/offers/offer_1509619452389.png", "itemName":"Panir Tikka", "netAmount":45, "offerId":"6d16df4a-ab9c-4930-8337-2b3f65969fb8", "quantity":3, "startValidity":"1970-01-01T01:30:00.000Z", "type":"offer", "unlimitedQuantity":false }, { "actualPrice":50, "dealPrice":45, "discountPercent":0, "endValidity":"1970-01-01T17:30:00.000Z", "itemImagePath":"https://s3.amazonaws.com/bigzpoon-develop/offers/offer_1509609642206.png", "itemName":"Panir Tikka", "netAmount":45, "offerId":"d4152ba9-a5bc-4f5b-ad03-a5180a0e0199", "quantity":11, "startValidity":"1970-01-01T01:30:00.000Z", "type":"offer", "unlimitedQuantity":true }, { "actualPrice":80, "dealPrice":80, "discountPercent":0, "endValidity":"1970-01-01T17:30:00.000Z", "itemImagePath":"menu/Burger_1507295095917.jpeg", "itemName":"Burger", "menuId":"5ea97c74-74fe-4e96-beb6-ba7772a160e3", "netAmount":80, "quantity":11, "startValidity":"1970-01-01T17:30:00.000Z", "type":"menu", "unlimitedQuantity":true }, { "actualPrice":50, "dealPrice":50, "discountPercent":0, "endValidity":"1970-01-01T17:30:00.000Z", "itemImagePath":"menu/Panir-Tikka_1507295128370.jpeg", "itemName":"Panir Tikka", "menuId":"70b4d4d3-2679-4e16-a90c-b081401c268f", "netAmount":50, "quantity":9, "startValidity":"1970-01-01T17:30:00.000Z", "type":"menu", "unlimitedQuantity":true } ], "orderNetAmount":"24.0", "orderQuantity":34, "payerId":"60g59yxg", "paymentMode":"paypal_account", "paymentRefKey":"60g59yxg", "paymentStatus":"1000", "paypalPaymentId":"60g59yxg", "restaurantId":"9b104614-b02f-45aa-ab6e-a2afd569835a", "restaurantName":"Michel Plaza", "salesTax":0, "serviceFee":156.8, "subTotal":1960, "transactionStatus":"settling", "userId":"538eb196-3f6a-415e-bd13-ffc4191c37b1" }, { "contactNumber":"8149425758", "couponcode":{ "midval":0, "type":"percent" }, "discount":0, "email":"udasibharat1@gmail.com", "firstName":"Bharat", "lastName":"Udasi", "orderLines":[ { "actualPrice":50, "dealPrice":45, "discountPercent":0, "endValidity":"1970-01-01T17:30:00.000Z", "itemImagePath":"https://s3.amazonaws.com/bigzpoon-develop/offers/offer_1509619452389.png", "itemName":"Panir Tikka", "netAmount":45, "offerId":"6d16df4a-ab9c-4930-8337-2b3f65969fb8", "quantity":3, "startValidity":"1970-01-01T01:30:00.000Z", "type":"offer", "unlimitedQuantity":false }, { "actualPrice":50, "dealPrice":45, "discountPercent":0, "endValidity":"1970-01-01T17:30:00.000Z", "itemImagePath":"https://s3.amazonaws.com/bigzpoon-develop/offers/offer_1509609642206.png", "itemName":"Panir Tikka", "netAmount":45, "offerId":"d4152ba9-a5bc-4f5b-ad03-a5180a0e0199", "quantity":11, "startValidity":"1970-01-01T01:30:00.000Z", "type":"offer", "unlimitedQuantity":true }, { "actualPrice":80, "dealPrice":80, "discountPercent":0, "endValidity":"1970-01-01T17:30:00.000Z", "itemImagePath":"menu/Burger_1507295095917.jpeg", "itemName":"Burger", "menuId":"5ea97c74-74fe-4e96-beb6-ba7772a160e3", "netAmount":80, "quantity":11, "startValidity":"1970-01-01T17:30:00.000Z", "type":"menu", "unlimitedQuantity":true }, { "actualPrice":50, "dealPrice":50, "discountPercent":0, "endValidity":"1970-01-01T17:30:00.000Z", "itemImagePath":"menu/Panir-Tikka_1507295128370.jpeg", "itemName":"Panir Tikka", "menuId":"70b4d4d3-2679-4e16-a90c-b081401c268f", "netAmount":50, "quantity":9, "startValidity":"1970-01-01T17:30:00.000Z", "type":"menu", "unlimitedQuantity":true } ], "orderNetAmount":"24.0", "orderQuantity":34, "payerId":"60g59yxg", "paymentMode":"paypal_account", "paymentRefKey":"60g59yxg", "paymentStatus":"1000", "paypalPaymentId":"60g59yxg", "restaurantId":"9b104614-b02f-45aa-ab6e-a2afd569835a", "restaurantName":"Michel Plaza", "salesTax":0, "serviceFee":156.8, "subTotal":1960, "transactionStatus":"settling", "userId":"538eb196-3f6a-415e-bd13-ffc4191c37b1" }, { "contactNumber":"8149425758", "couponcode":{ "midval":0, "type":"percent" }, "discount":0, "email":"udasibharat1@gmail.com", "firstName":"Bharat", "lastName":"Udasi", "orderLines":[ { "actualPrice":50, "dealPrice":45, "discountPercent":0, "endValidity":"1970-01-01T17:30:00.000Z", "itemImagePath":"https://s3.amazonaws.com/bigzpoon-develop/offers/offer_1509619452389.png", "itemName":"Panir Tikka", "netAmount":45, "offerId":"6d16df4a-ab9c-4930-8337-2b3f65969fb8", "quantity":3, "startValidity":"1970-01-01T01:30:00.000Z", "type":"offer", "unlimitedQuantity":false }, { "actualPrice":50, "dealPrice":45, "discountPercent":0, "endValidity":"1970-01-01T17:30:00.000Z", "itemImagePath":"https://s3.amazonaws.com/bigzpoon-develop/offers/offer_1509609642206.png", "itemName":"Panir Tikka", "netAmount":45, "offerId":"d4152ba9-a5bc-4f5b-ad03-a5180a0e0199", "quantity":11, "startValidity":"1970-01-01T01:30:00.000Z", "type":"offer", "unlimitedQuantity":true }, { "actualPrice":80, "dealPrice":80, "discountPercent":0, "endValidity":"1970-01-01T17:30:00.000Z", "itemImagePath":"menu/Burger_1507295095917.jpeg", "itemName":"Burger", "menuId":"5ea97c74-74fe-4e96-beb6-ba7772a160e3", "netAmount":80, "quantity":11, "startValidity":"1970-01-01T17:30:00.000Z", "type":"menu", "unlimitedQuantity":true }, { "actualPrice":50, "dealPrice":50, "discountPercent":0, "endValidity":"1970-01-01T17:30:00.000Z", "itemImagePath":"menu/Panir-Tikka_1507295128370.jpeg", "itemName":"Panir Tikka", "menuId":"70b4d4d3-2679-4e16-a90c-b081401c268f", "netAmount":50, "quantity":9, "startValidity":"1970-01-01T17:30:00.000Z", "type":"menu", "unlimitedQuantity":true } ], "orderNetAmount":"24.0", "orderQuantity":34, "payerId":"60g59yxg", "paymentMode":"paypal_account", "paymentRefKey":"60g59yxg", "paymentStatus":"1000", "paypalPaymentId":"60g59yxg", "restaurantId":"9b104614-b02f-45aa-ab6e-a2afd569835a", "restaurantName":"Michel Plaza", "salesTax":0, "serviceFee":156.8, "subTotal":1960, "transactionStatus":"settling", "userId":"538eb196-3f6a-415e-bd13-ffc4191c37b1" }, { "contactNumber":"8149425758", "couponcode":{ "midval":0, "type":"percent" }, "discount":0, "email":"udasibharat1@gmail.com", "firstName":"Bharat", "lastName":"Udasi", "orderLines":[ { "actualPrice":50, "dealPrice":45, "discountPercent":0, "endValidity":"1970-01-01T17:30:00.000Z", "itemImagePath":"https://s3.amazonaws.com/bigzpoon-develop/offers/offer_1509619452389.png", "itemName":"Panir Tikka", "netAmount":45, "offerId":"6d16df4a-ab9c-4930-8337-2b3f65969fb8", "quantity":3, "startValidity":"1970-01-01T01:30:00.000Z", "type":"offer", "unlimitedQuantity":false }, { "actualPrice":50, "dealPrice":45, "discountPercent":0, "endValidity":"1970-01-01T17:30:00.000Z", "itemImagePath":"https://s3.amazonaws.com/bigzpoon-develop/offers/offer_1509609642206.png", "itemName":"Panir Tikka", "netAmount":45, "offerId":"d4152ba9-a5bc-4f5b-ad03-a5180a0e0199", "quantity":11, "startValidity":"1970-01-01T01:30:00.000Z", "type":"offer", "unlimitedQuantity":true }, { "actualPrice":80, "dealPrice":80, "discountPercent":0, "endValidity":"1970-01-01T17:30:00.000Z", "itemImagePath":"menu/Burger_1507295095917.jpeg", "itemName":"Burger", "menuId":"5ea97c74-74fe-4e96-beb6-ba7772a160e3", "netAmount":80, "quantity":11, "startValidity":"1970-01-01T17:30:00.000Z", "type":"menu", "unlimitedQuantity":true }, { "actualPrice":50, "`enter code here`dealPrice":50, "discountPercent":0, "endValidity":"1970-01-01T17:30:00.000Z", "itemImagePath":"menu/Panir-Tikka_1507295128370.jpeg", "itemName":"Panir Tikka", "menuId":"70b4d4d3-2679-4e16-a90c-b081401c268f", "netAmount":50, "quantity":9, "startValidity":"1970-01-01T17:30:00.000Z", "type":"menu", "unlimitedQuantity":true } ], "orderNetAmount":"24.0", "orderQuantity":34, "payerId":"60g59yxg", "paymentMode":"paypal_account", "paymentRefKey":"60g59yxg", "paymentStatus":"1000", "paypalPaymentId":"60g59yxg", "restaurantId":"9b104614-b02f-45aa-ab6e-a2afd569835a", "restaurantName":"Michel Plaza", "salesTax":0, "serviceFee":156.8, "subTotal":1960, "transactionStatus":"settling", "userId":"538eb196-3f6a-415e-bd13-ffc4191c37b1" } ] 生成结构,如下所示

CucumberWithSerenity.class

1 个答案:

答案 0 :(得分:0)

你已经完成了所有部分。你需要做的最后一件事,将gson字符串转换为JSONArray。使用此代码

JSONArray jsonarray = new JSONArray("gson string");

如果你需要这样的话

{
   "restaurantId":"0a60fa5e-642c-4e4f-ae37-a7c8093e1659",
      '
      '
      '
      '
      '
   "contactNumber":"9516238470",
   "couponcode":{
      '
      '
      '
      '
   },
   "orderLines":[
      '
      '
      '
      '
   ]
}

不喜欢这个

{
   "contactNumber":"8149425758",
      '
      '
      '
      '
      '
   "couponcode":{
      '
      '
      '
      '
   },
   "lastName":"Udasi",
   "orderLines":[
      '
      '
      '
      '
   ],
   "orderNetAmount":"24.0",
      '
      '
      '
}

这是因为按元素键排序的数据。看看元素键然后你可以看到