dongyuan1970 2016-09-04 17:11
浏览 56
已采纳

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

I am writing a middleware for a GoLang application (http://hoverfly.io/) but having some issues.

The concept is very simple. Golang passes in a JSON input to my Java middleware, and my middleware then returns a modified json output to the Golang application.

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();
    }



  }    
}

I then run the program:

./hoverfly -middleware "java -jar mid.jar"

The JSON input to my program is this:

{"response":{"status":200,"body":"\u003c?xml version=\"1.0\"?\u003e\u003cpathList xmlns:xlink=\"http://www.w3.org/1999/xlink\"\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/0/\"\u003e0\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/1/\"\u003e1\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/3/\"\u003e3\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/4/\"\u003e4\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/5/\"\u003e5\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/6/\"\u003e6\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/7/\"\u003e7\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/8/\"\u003e8\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/9/\"\u003e9\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/10/\"\u003e10\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/11/\"\u003e11\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/12/\"\u003e12\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/13/\"\u003e13\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/14/\"\u003e14\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/15/\"\u003e15\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/16/\"\u003e16\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/17/\"\u003e17\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/18/\"\u003e18\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/19/\"\u003e19\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/20/\"\u003e20\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/21/\"\u003e21\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/22/\"\u003e22\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/23/\"\u003e23\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/25/\"\u003e25\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/26/\"\u003e26\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/27/\"\u003e27\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/28/\"\u003e28\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/29/\"\u003e29\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/30/\"\u003e30\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/31/\"\u003e31\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/32/\"\u003e32\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/33/\"\u003e33\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/34/\"\u003e34\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/35/\"\u003e35\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/36/\"\u003e36\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/37/\"\u003e37\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/38/\"\u003e38\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/39/\"\u003e39\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/40/\"\u003e40\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/41/\"\u003e41\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/42/\"\u003e42\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/43/\"\u003e43\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/44/\"\u003e44\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/45/\"\u003e45\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/46/\"\u003e46\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/47/\"\u003e47\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/48/\"\u003e48\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/49/\"\u003e49\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/50/\"\u003e50\u003c/path\u003e
    \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/5555/\"\u003e5555\u003c/path\u003e
\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"]}}}

The output sent to hoverfly is then this:

{"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\">
    <endpoint xlink:href=\"http://testapi/path/endpoint/0/\">0<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/1/\">1<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/3/\">3<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/4/\">4<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/5/\">5<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/6/\">6<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/7/\">7<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/8/\">8<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/9/\">9<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/10/\">10<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/11/\">11<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/12/\">12<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/13/\">13<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/14/\">14<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/15/\">15<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/16/\">16<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/17/\">17<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/18/\">18<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/19/\">19<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/20/\">20<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/21/\">21<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/22/\">22<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/23/\">23<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/25/\">25<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/26/\">26<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/27/\">27<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/28/\">28<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/29/\">29<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/30/\">30<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/31/\">31<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/32/\">32<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/33/\">33<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/34/\">34<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/35/\">35<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/36/\">36<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/37/\">37<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/38/\">38<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/39/\">39<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/40/\">40<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/41/\">41<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/42/\">42<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/43/\">43<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/44/\">44<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/45/\">45<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/46/\">46<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/47/\">47<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/48/\">48<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/49/\">49<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/50/\">50<\/endpoint>
    <endpoint xlink:href=\"http://testapi/path/endpoint/5555/\">5555<\/endpoint>
<\/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"}}

However when I run this I get the error:

"error":"json: cannot unmarshal string into Go value of type int","level":"error","msg":"Failed to unmarshal JSON 

Is Go expecting some kind of special JSON format or what can the problem be?

All help is appreciated.

  • 写回答

1条回答 默认 最新

  • dongxun5349 2016-09-04 17:35
    关注

    The error message says cannot unmarshal string into Go value of type int. Which means you are sending something as string when Go is expecting it to be an int.

    The Go code is trying to unmarshall JSON using this definition

    Solution:

    "status":"500" should be sent as "status":500

    Try

     response.put("status", 500);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接
  • ¥15 MATLAB中使用parfor,矩阵Removal的有效索引在parfor循环中受限制
  • ¥20 Win 10 LTSC 1809版本如何无损提升到20H1版本
  • ¥50 win10 LTSC 虚拟键盘不弹出
  • ¥30 微信小程序请求失败,网页能正常带锁访问
  • ¥15 Matlab求解微分方程,如何用fish2d进行预优?