簡體   English   中英

Golang / Java無法從中間件解組JSON

[英]Golang/Java Failed to unmarshal JSON from 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格式嗎?問題可能是什么?

感謝所有幫助。

錯誤消息說cannot unmarshal string into Go value of type int 這意味着當Go期望它是一個int時,您正在以string形式發送某些內容。

Go代碼正在嘗試使用定義解組JSON

解:

"status":"500"應作為"status":500

嘗試

 response.put("status", 500);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM