Golang / Java无法从中间件解组JSON

时间:2016-09-04 17:11:29

标签: java json go middleware

我正在为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格式或问题是什么?

感谢所有帮助。

1 个答案:

答案 0 :(得分:3)

错误消息显示cannot unmarshal string into Go value of type int。这意味着,当Go期望它为string时,您发送的内容为int

Go代码正在尝试使用this定义

解组JSON

<强>解决方案:

"status":"500"应以"status":500

的形式发送

尝试

 response.put("status", 500);