比较来自世界各地的卖家的域名和 IT 服务价格

发送数据 json 通过使用模板的邮政呼叫 rest 的 spring

我经常出错言说这是错误的 json, 并继续弄错 400. 我认为我没有正确格式化数据的实现。
目的:尝试使用模板进行拨打电话 rest, 传输数据 JSON 作为一个身体。 似乎在转换数据时 map 在 json 它们被错误地转化为 json.


public void GetJsonData/String name, String city/ {
Map<string, string=""> map = new HashMap<string, string="">//;
map.put/"api_key", apikey/;



map.put/"venue_queries", "[{'name':'"+name+"', 'location': {'locality': '"+city+"'}}]"/;

try {
String resp = GlobalHelper.calltestService/RestServicesUrl, map/;
System.out.println/resp/;
} catch /Exception e/ {
// TODO Auto-generated catch block
e.printStackTrace//;
}
}


public String calltestService/String url, Map<string, string=""> data/ throws Exception {
RestTemplate rest = new RestTemplate//;
HttpHeaders headers = new HttpHeaders//;
headers.setContentType/MediaType.APPLICATION_JSON/;
headers.setAccept/Arrays.asList/MediaType.APPLICATION_JSON//;
ResponseEntity<string> response = new ResponseEntity<string>/headers, HttpStatus.OK/;
rest.getMessageConverters//.add/new MappingJackson2HttpMessageConverter///;
Gson gson = new Gson//;
System.out.println/gson.toJson/data//;
try {
response = rest.postForEntity/url, gson.toJson/data/, String.class/;
System.out.println/response.getBody///;
System.out.println/response.getHeaders///;
return response.getBody//;
} catch /Exception e/ {
e.printStackTrace//;
System.out.println/"error in post entity"/;
return "error";
}
}


</string></string></string,></string,></string,>
已邀请:

江南孤鹜

赞同来自:

对,你的结果 JSON 无效的


{
"venue_queries": "[{\u0027name\u0027:\u0027yo\u0027, \u0027location\u0027: {\u0027locality\u0027: \u0027Solares\u0027}}]",
"api_key": "asfasdfasfdasdfasdfa"
}


你 JSON 无效,因为 GSON 屏幕符号 HTML 默认值并将单引号放置为等效符号代码 unicode U+0027

如果你禁用
http://google-gson.googlecode. ... %2529

GsonBuilder builder = new GsonBuilder//;
builder.disableHtmlEscaping//;
Gson gson = builder.create//;


你会得到


{
"venue_queries": "[{'name':'yo', 'location': {'locality': 'Solares'}}]",
"api_key": "asfasdfasfdasdfasdfa"
}


无论如何,你的 JSON 还是错了。

你必须纠正你的
venue_queries

地图上的主要内容
using double quotes


"



map.put/"venue_queries", "[{\"name\":\""+name+"\", \"location\": {\"locality\": \""+city+"\"}}]"/;


然后是你的结果 JSON 将有效


{
"venue_queries": "[{\"name\":\"yo\", \"location\": {\"locality\": \"Solares\"}}]",
"api_key": "asfasdfasfdasdfasdfa"
}


分段 Tes


import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.google.gson.Gson;

public class GsonMap {

public static void main/String[] args/ {
Map<string, string=""> map = new HashMap<string, string="">//;
String apikey = "asfasdfasfdasdfasdfa";
map.put/"api_key", apikey/;
String name = "yo";
String city = "Solares";
map.put/"venue_queries", "[{\"name\":\""+name+"\", \"location\": {\"locality\": \""+city+"\"}}]"/;
Gson gson = new Gson//;
String json = gson.toJson/map/;
System.out.println/"Resulting JSON:" + json/;
map = gson.fromJson/json, map.getClass///;
System.out.println/"Resulting map values:"/;
for /Entry<string, string=""> entry : map.entrySet/// {
System.out.println/"Key:" + entry.getKey// + ", value:" + entry.getValue///;
}
}
}


</string,></string,></string,>

要回复问题请先登录注册