- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在为 GoLang 应用程序 ( http://hoverfly.io/ ) 编写中间件,但遇到了一些问题。
这个概念很简单。 Golang 将 JSON 输入传递到我的 Java 中间件,然后我的中间件将修改后的 json 输出返回到 Golang 应用程序。
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import org.json.JSONObject;
public class mid
{
static String payload ="";
public static void main (String[] args) throws Exception
{
try {
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
payload = scanner.nextLine();
JSONObject obj = new JSONObject(payload);
JSONObject response = obj.getJSONObject("response");
response.remove("status");
response.put("status", "500");
String newjson = obj.toString();
write (newjson);
System.out.print(obj);
} catch (Exception e) {
e.printStackTrace();
}
}
}
然后我运行程序:
./hoverfly -middleware "java -jar mid.jar"
我程序的 JSON 输入是这样的:
{"response":{"status":200,"body":"\u003c?xml version=\"1.0\"?\u003e\u003cpathList xmlns:xlink=\"http://www.w3.org/1999/xlink\"\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/0/\"\u003e0\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/1/\"\u003e1\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/3/\"\u003e3\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/4/\"\u003e4\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/5/\"\u003e5\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/6/\"\u003e6\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/7/\"\u003e7\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/8/\"\u003e8\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/9/\"\u003e9\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/10/\"\u003e10\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/11/\"\u003e11\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/12/\"\u003e12\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/13/\"\u003e13\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/14/\"\u003e14\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/15/\"\u003e15\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/16/\"\u003e16\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/17/\"\u003e17\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/18/\"\u003e18\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/19/\"\u003e19\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/20/\"\u003e20\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/21/\"\u003e21\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/22/\"\u003e22\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/23/\"\u003e23\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/25/\"\u003e25\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/26/\"\u003e26\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/27/\"\u003e27\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/28/\"\u003e28\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/29/\"\u003e29\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/30/\"\u003e30\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/31/\"\u003e31\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/32/\"\u003e32\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/33/\"\u003e33\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/34/\"\u003e34\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/35/\"\u003e35\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/36/\"\u003e36\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/37/\"\u003e37\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/38/\"\u003e38\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/39/\"\u003e39\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/40/\"\u003e40\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/41/\"\u003e41\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/42/\"\u003e42\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/43/\"\u003e43\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/44/\"\u003e44\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/45/\"\u003e45\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/46/\"\u003e46\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/47/\"\u003e47\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/48/\"\u003e48\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/49/\"\u003e49\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/50/\"\u003e50\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/5555/\"\u003e5555\u003c/path\u003e\n\u003c/pathList\u003e","encodedBody":false,"headers":{"Content-Length":["4580"],"Content-Type":["application/xml"],"Date":["Fri, 02 Sep 2016 11:31:17 GMT"],"Hoverfly":["Was-Here"],"Server":["Apache-Coyote/1.1"]}},"request":{"requestType":"recording","path":"/endpoint/path/","method":"GET","destination":"www.testapi.com","scheme":"http","query":"","body":"","headers":{"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"],"Accept-Encoding":["gzip, deflate"],"Accept-Language":["en-US,sv-SE;q=0.8,sv;q=0.5,en;q=0.3"],"Connection":["keep-alive"],"User-Agent":["Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:47.0) Gecko/20100101 Firefox/47.0"]}}}
然后发送到 hoverfly 的输出是这样的:
{"response":{"headers":{"Hoverfly":["Was-Here"],"Date":["Fri, 02 Sep 2016 11:31:17 GMT"],"Content-Length":["4580"],"Content-Type":["application/xml"],"Server":["Apache-Coyote/1.1"]},"body":"<?xml version=\"1.0\"?><endpointList xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n <endpoint xlink:href=\"http://testapi/path/endpoint/0/\">0<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/1/\">1<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/3/\">3<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/4/\">4<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/5/\">5<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/6/\">6<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/7/\">7<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/8/\">8<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/9/\">9<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/10/\">10<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/11/\">11<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/12/\">12<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/13/\">13<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/14/\">14<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/15/\">15<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/16/\">16<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/17/\">17<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/18/\">18<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/19/\">19<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/20/\">20<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/21/\">21<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/22/\">22<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/23/\">23<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/25/\">25<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/26/\">26<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/27/\">27<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/28/\">28<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/29/\">29<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/30/\">30<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/31/\">31<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/32/\">32<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/33/\">33<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/34/\">34<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/35/\">35<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/36/\">36<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/37/\">37<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/38/\">38<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/39/\">39<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/40/\">40<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/41/\">41<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/42/\">42<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/43/\">43<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/44/\">44<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/45/\">45<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/46/\">46<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/47/\">47<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/48/\">48<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/49/\">49<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/50/\">50<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/5555/\">5555<\/endpoint>\n<\/endpointList>","status":"500","encodedBody":false},"request":{"headers":{"Accept-Language":["en-US,sv-SE;q=0.8,sv;q=0.5,en;q=0.3"],"Accept-Encoding":["gzip, deflate"],"User-Agent":["Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:47.0) Gecko/20100101 Firefox/47.0"],"Connection":["keep-alive"],"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"]},"body":"","scheme":"http","query":"","path":"/path/endpoint/","method":"GET","requestType":"recording","destination":"testapi"}}
但是,当我运行它时,出现错误:
"error":"json: cannot unmarshal string into Go value of type int","level":"error","msg":"Failed to unmarshal JSON
Go 是否需要某种特殊的 JSON 格式或者可能是什么问题?
感谢所有帮助。
最佳答案
错误消息说无法将字符串解码为 int 类型的 Go 值
。这意味着当 Go 期望它是 int
时,您发送的是 string
。
Go 代码正在尝试使用 this 解码 JSON定义
解决方案:
"status":"500"
应作为 "status":500
尝试
response.put("status", 500);
关于java - Golang/Java 无法从中间件中解码 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39319481/
最近开始学习MongoDB。今天老师教了我们 mongoexport 命令。在练习时,我遇到了一个典型的问题,包括教练在内的其他同学都没有遇到过。我在我的 Windows 10 机器上使用 Mongo
我是 JSON Schema 的新手,读过什么是 JSON Schema 等等。但我不知道如何将 JSON Schema 链接到 JSON 以针对该 JSON Schema 进行验证。谁能解释一下?
在 xml 中,我可以在另一个 xml 文件中包含一个文件并使用它。如果您的软件从 xml 获取配置文件但没有任何方法来分离配置,如 apache/ngnix(nginx.conf - site-av
我有一个 JSON 对象,其中包含一个本身是 JSON 对象的字符串。我如何反序列化它? 我希望能够做类似的事情: #[derive(Deserialize)] struct B { c: S
考虑以下 JSON { "a": "{\"b\": 12, \"c\": \"test\"}" } 我想定义一个泛型读取 Reads[Outer[T]]对于这种序列化的 Json import
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 11 个月前关闭。 Improve
我的旧项目在 MySQL 中有 Standard JSON 格式的数据。 对于我在 JS (Node.js) 和 DynamoDB 中的全新项目,关于 Standard JSON格式: 是否建议将其转
JSON 值字符串、数字、true、false、null 是否是有效的 JSON? 即,是 true 一个有效的 JSON 文档?还是必须是数组/对象? 一些验证器接受这个(例如 http://jso
我有一个 JSON 字符串,其中一个字段是文本字段。这个文本字段可以包含用户在 UI 中输入的文本,如果他们输入的文本是 JSON 文本,也许是为了说明一些编码,我需要对他们的文本进行编码,以便它不会
我正在通过 IBM MQ 调用处理数据,当由 ColdFusion 10 (10,0,11,285437) 序列化时,0 将作为 +0.0 返回,它会导致无效的 JSON并且无法反序列化。 stPol
我正在从三个数组中生成一个散列,然后尝试构建一个 json。我通过 json object has array 成功了。 require 'json' A = [['A1', 'A2', 'A3'],
我从 API 接收 JSON,响应可以是 30 种类型之一。每种类型都有一组唯一的字段,但所有响应都有一个字段 type 说明它是哪种类型。 我的方法是使用serde .我为每种响应类型创建一个结构并
我正在下载一个 JSON 文件,我已将其检查为带有“https://jsonlint.com”的有效 JSON 到文档目录。然后我打开文件并再次检查,结果显示为无效的 JSON。这怎么可能????这是
我正在尝试根据从 API 接收到的数据动态创建一个 JSON 对象。 收到的示例数据:将数据解码到下面给出的 CiItems 结构中 { "class_name": "test", "
我想从字符串转换为对象。 来自 {"key1": "{\n \"key2\": \"value2\",\n \"key3\": {\n \"key4\": \"value4\"\n }\n
目前我正在使用以下代码将嵌套的 json 转换为扁平化的 json: import ( "fmt" "github.com/nytlabs/gojsonexplode" ) func
我有一个使用来自第三方 API 的数据的应用程序。我需要将 json 解码为一个结构,这需要该结构具有“传入”json 字段的 json 标签。传出的 json 字段具有不同的命名约定,因此我需要不同
我想使用 JSON 架构来验证某些值。我有两个对象,称它们为 trackedItems 和 trackedItemGroups。 trackedItemGroups 是组名称和 trackedItem
考虑以下案例类模式, case class Y (a: String, b: String) case class X (dummy: String, b: Y) 字段b是可选的,我的一些数据集没有字
我正在存储 cat ~/path/to/file/blah | 的输出jq tojson 在一个变量中,稍后在带有 JSON 内容的 curl POST 中使用。它运作良好,但它删除了所有换行符。我知
我是一名优秀的程序员,十分优秀!