首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编辑json文本http3

编辑json文本http3
EN

Stack Overflow用户
提问于 2019-07-25 01:26:09
回答 2查看 51关注 0票数 0

我有一个带有json的post函数。我想知道您是否可以使用“编辑文本”来更改json中的值。

在json中有一个部分有一个总数,我想用编辑文本来改变这个总数。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-07-25 04:25:22

可以,停那儿吧。您基本上需要获取EditText中的值,然后可以使用JSONObjectput方法将该值赋给json。(如果您的json是一个字符串,则需要创建一个JSONObject from the json string

你基本上需要按照下面的思路做一些事情,

代码语言:javascript
复制
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()
        )
票数 0
EN

Stack Overflow用户

发布于 2019-07-25 04:46:47

下面是一些代码。

代码语言:javascript
复制
        //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}"
        )
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57188299

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档