我有一个带有json的post函数。我想知道您是否可以使用“编辑文本”来更改json中的值。
在json中有一个部分有一个总数,我想用编辑文本来改变这个总数。
发布于 2019-07-25 04:25:22
可以,停那儿吧。您基本上需要获取EditText中的值,然后可以使用JSONObject的put方法将该值赋给json。(如果您的json是一个字符串,则需要创建一个JSONObject from the json string
你基本上需要按照下面的思路做一些事情,
val mediaType = MediaType.parse("application/json")
//Create JSONObject from json string
val jsonObject = JSONObject("{\r\n \"dateTime\": \"2019-07-01T00:00:00-07:00\",\r\n \"apiOptions\": [\r\n \"ALLOWPARTIALAUTH\"\r\n ],\r\n \"amount\": {\r\n \"cashback\": 20,\r\n \"surcharge\": 5,\r\n \"tax\": 15,\r\n \"tip\": 20,\r\n \"total\": 160\r\n },\r\n \"card\": {\r\n \"entryMode\": \"M\",\r\n \"expirationDate\": 1230,\r\n \"number\": \"4321000000001119\",\r\n \"present\": \"N\",\r\n \"securityCode\": {\r\n \"indicator\": \"1\",\r\n \"value\": \"333\"\r\n }\r\n },\r\n \"clerk\": {\r\n \"numericId\": 1576\r\n },\r\n \"customer\": {\r\n \"addressLine1\": \"65 Easy St\",\r\n \"firstName\": \"John\",\r\n \"lastName\": \"Smith\",\r\n \"postalCode\": \"65144\"\r\n },\r\n \"transaction\": {\r\n \"invoice\": \"192029\",\r\n \"notes\": \"Transaction notes are added here\",\r\n \"hotel\": {\r\n \"arrivalDateTime\": \"2018-06-18T15:39:01.594-07:00\",\r\n \"departureDateTime\": \"2018-06-21T09:18:23.283-07:00\",\r\n \"primaryChargeType\": 1,\r\n \"specialCode\": 1,\r\n \"additionalCharges\": {\r\n \"giftShop\": \"Y\",\r\n \"laundry\": \"Y\",\r\n \"miniBar\": \"Y\",\r\n \"other\": \"Y\",\r\n \"restaurant\": \"Y\",\r\n \"telephone\": \"Y\"\r\n },\r\n \"roomRates\": [\r\n {\r\n \"nights\": 2,\r\n \"rate\": 159.95\r\n },\r\n {\r\n \"nights\": 3,\r\n \"rate\": 125.38\r\n }\r\n ]\r\n },\r\n \"purchaseCard\": {\r\n \"customerReference\": \"D019D09309F2\",\r\n \"destinationPostalCode\": \"94719\",\r\n \"productDescriptors\": [\r\n \"Hamburger\",\r\n \"Fries\",\r\n \"Soda\",\r\n \"Cookie\"\r\n ]\r\n }\r\n },\r\n \"lighthouse\": {\r\n \"data\": \"eyJsaWdodGhvdXNlIjp7ImVtcGxveWVlaWQiOjEyMzQsImRldmljZWlkIjoiMTIzU0FCViJ9fQ==\"\r\n }\r\n}")
//Get numerical value from EditText
val newTotal:Int? = editText.text.toString().toIntOrNull()
if (newTotal != null) {
//If it's a valid Int value, then put it in the JSONObject
//Note - 'total' is inside another json given by the value 'amount'
jsonObject.getJSONObject("amount").put("total", newTotal)
}
val body = RequestBody.create(
mediaType,
jsonObject.toString()
)发布于 2019-07-25 04:46:47
下面是一些代码。
//this tells the program that it is going to be using http3 to post the api
val client = OkHttpClient()
//This chooses what is going to be sent to the api. The media type is basically if it is going to be text javascript kotlin json ect.
val mediaType = MediaType.parse("application/json")
//This is where the body is created or at least ready to form. What is does is set up a value for where you input the json you want to send to the api.
val body = RequestBody.create(
mediaType,
"{\r\n \"dateTime\": \"2019-07-01T00:00:00-07:00\",\r\n \"apiOptions\": [\r\n \"ALLOWPARTIALAUTH\"\r\n ],\r\n \"amount\": {\r\n \"cashback\": 20,\r\n \"surcharge\": 5,\r\n \"tax\": 15,\r\n \"tip\": 20,\r\n \"total\": 160\r\n },\r\n \"card\": {\r\n \"entryMode\": \"M\",\r\n \"expirationDate\": 1230,\r\n \"number\": \"4321000000001119\",\r\n \"present\": \"N\",\r\n \"securityCode\": {\r\n \"indicator\": \"1\",\r\n \"value\": \"333\"\r\n }\r\n },\r\n \"clerk\": {\r\n \"numericId\": 1576\r\n },\r\n \"customer\": {\r\n \"addressLine1\": \"65 Easy St\",\r\n \"firstName\": \"John\",\r\n \"lastName\": \"Smith\",\r\n \"postalCode\": \"65144\"\r\n },\r\n \"transaction\": {\r\n \"invoice\": \"192029\",\r\n \"notes\": \"Transaction notes are added here\",\r\n \"hotel\": {\r\n \"arrivalDateTime\": \"2018-06-18T15:39:01.594-07:00\",\r\n \"departureDateTime\": \"2018-06-21T09:18:23.283-07:00\",\r\n \"primaryChargeType\": 1,\r\n \"specialCode\": 1,\r\n \"additionalCharges\": {\r\n \"giftShop\": \"Y\",\r\n \"laundry\": \"Y\",\r\n \"miniBar\": \"Y\",\r\n \"other\": \"Y\",\r\n \"restaurant\": \"Y\",\r\n \"telephone\": \"Y\"\r\n },\r\n \"roomRates\": [\r\n {\r\n \"nights\": 2,\r\n \"rate\": 159.95\r\n },\r\n {\r\n \"nights\": 3,\r\n \"rate\": 125.38\r\n }\r\n ]\r\n },\r\n \"purchaseCard\": {\r\n \"customerReference\": \"D019D09309F2\",\r\n \"destinationPostalCode\": \"94719\",\r\n \"productDescriptors\": [\r\n \"Hamburger\",\r\n \"Fries\",\r\n \"Soda\",\r\n \"Cookie\"\r\n ]\r\n }\r\n },\r\n \"lighthouse\": {\r\n \"data\": \"eyJsaWdodGhvdXNlIjp7ImVtcGxveWVlaWQiOjEyMzQsImRldmljZWlkIjoiMTIzU0FCViJ9fQ==\"\r\n }\r\n}"
)https://stackoverflow.com/questions/57188299
复制相似问题