java 使用邮递员的 json 响应中的错误字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42546505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:42:56  来源:igfitidea点击:

Bad String in json response using Post man

javajsonspring

提问by daisy

I am Returning string as shown below from a method on service call, when i call this service in Postman it returns Bad String.

我正在从服务调用的方法中返回如下所示的字符串,当我在 Postman 中调用此服务时,它返回Bad String

@Override
public synchronized String doDevicePair() throws SomeException    
{
    String returnString = null;     
      /*some logic over here*/

             returnString = "{\"success\": true,   \"payload\": \"success!\", "
            + "{\"userIdA\":\""+user1.getUserId()+"\", \"userIdB\":\""+user2.getUserId()+"\", \"tagName\":\""+tagName+"\" }"+"}";

     return returnString;
      }

I think its giving Bad String response because of Wrong syntax of return String. so i tried

我认为由于返回字符串的语法错误,它给出了错误的字符串响应。所以我试过了

returnString = "{\"success\": true,   \"payload\": \"success!\", "
            + "{\"userIdA\":\""+user1.getUserId()+"\", \"userIdB\":\""+user2.getUserId()+"\", \"tagName\":\""+tagName+"\" }}";

//gives bad string reponse

//给出错误的字符串响应

so i just checked by returning hardcoded string

所以我只是通过返回硬编码字符串来检查

returnString = "{\"success\": true,   \"payload\": \"succes\"  }";

This produces json response on service call , without any error

这会在服务调用时产生 json 响应,没有任何错误

Whats going wrong? Please help me on this

怎么了?请帮我解决这个问题

回答by callmepills

You are missing the string name for the inner object. Perhaps it should be:

您缺少内部对象的字符串名称。也许应该是:

returnString = 
    "{\"success\": true,   \"payload\": \"success!\", \"foo\": " // <-- missing property name
        + "{\"userIdA\":\"" + user1.getUserId()
        + "\", \"userIdB\":\"" + user2.getUserId()
        + "\", \"tagName\":\"" + tagName + "\" }"
    + "}";

回答by june song

I think when you use JSON object, good to check the formatting first on some sites. There are bunch of good sites for that like here. JSON ONLINE EDITOR. Also, you would better to use for the JSON string by using JSON library like JSON Simple.

我认为当您使用 JSON 对象时,最好先在某些站点上检查格式。有很多像这里这样的好网站。JSON 在线编辑器。此外,您最好通过使用像 JSON Simple 这样的 JSON 库来使用 JSON 字符串。

回答by alok

I also got the same error in post but it resolved now. you can run one method where you get the response in json and then that same response you can you for post body data.

我在帖子中也遇到了同样的错误,但现在解决了。您可以运行一种方法,在该方法中您获得 json 中的响应,然后您可以使用相同的响应来发布正文数据。