【问题标题】:Correct JSON Format For http, Correct JSON Escape Chartershttp 的正确 JSON 格式,正确的 JSON Escape Charters
【发布时间】:2015-09-08 19:53:05
【问题描述】:

刚开始使用 JSON。已经构建了一个 Spring web-app。目前使用 Chrome 的 Advanced Rest Client 来生成一些内容。

我的问题是,如何正确格式化我的 JSON 以免收到错误?

下面我包含了您可能会发现有用的代码和信息。

高级 Rest 客户端:

https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo?hl=en-US

http 有一些格式问题。此处显示:

"tutorialUrls": ["https://jersey.java.net/","https://en.wikipedia.org/wiki/Project_Jersey","http://www.mkyong.com/tutorials/jax-rs-tutorials/","https://docs.oracle.com/cd/E19776-01/820-4867/6nga7f5o5/index.html","https://www.youtube.com/watch?v=sVvL12BnIyQ","https://www.youtube.com/watch?v=_AueeTNJsyk"],

此处显示完整的有效负载:

{
 "kitName": "JERSEY Kit",
 "createdDate": "9\/7\/2015",
 "createdBy": "Reed Williams",
 "description": "JERSEY: RESTful Web Services In Java",
 "category": "jersey",
 "tutorialUrls": ["https://jersey.java.net/","https://en.wikipedia.org/wiki/Project_Jersey","http://www.mkyong.com/tutorials/jax-rs-tutorials/","https://docs.oracle.com/cd/E19776-01/820-4867/6nga7f5o5/index.html","https://www.youtube.com/watch?v=sVvL12BnIyQ","https://www.youtube.com/watch?v=_AueeTNJsyk"],
 "kitNotes": Jersey RESTful Web Services framework is an open source, production quality framework for developing RESTful Web Services in Java that provides support for JAX-RS APIs and serves as a JAX-RS (JSR 311 & JSR 339) Reference Implementation.
}

此处显示完整回复:

{
timestamp: 1441740764717
status: 400
error: "Bad Request"
exception: "org.springframework.http.converter.HttpMessageNotReadableException"
message: "Could not read document: Unrecognized token 'Jersey': was expecting ('true', 'false' or 'null') at [Source: java.io.PushbackInputStream@347e1916; line: 9, column: 21]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'Jersey': was expecting ('true', 'false' or 'null') at [Source: java.io.PushbackInputStream@347e1916; line: 9, column: 21]"
path: "/addNewKit"
}

此外,这是我在 Advanced Rest Client 上使用的屏幕截图:

http://screencast.com/t/47jq1T2l

这是相关的Java模型,StarterKitInfo.java:

package com.reedwilliams.techbitstarterkit.models;

import java.util.List;

/**
 *
 */
public class StarterKitInfo {

	private String kitName;
	private String createdDate;
	private String createdBy;
	private String description;
	private String category;
	private List<String> tutorialUrls;
//	private List<String> tutorialNotes;
	private String kitNotes;
	private String id;

	/**
	 *
	 * @return
	 */
	public String getKitName() {
		return kitName;
	}

	/**
	 *
	 * @param kitName
	 */
	public void setKitName(String kitName) {
		this.kitName = kitName;
	}

	/**
	 *
	 * @return
	 */
	public String getCreatedDate() {
		return createdDate;
	}

	/**
	 *
	 * @param createdDate
	 */
	public void setCreatedDate(String createdDate) {
		this.createdDate = createdDate;
	}

	/**
	 *
	 * @return
	 */
	public String getCreatedBy() {
		return createdBy;
	}

	public void setCreatedBy(String createdBy) {
		this.createdBy = createdBy;
	}

	/**
	 *
	 * @return
	 */
	public String getDescription() {
		return description;
	}

	/**
	 *
	 * @param description
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	/**
	 *
	 * @return
	 */
	public String getCategory() {
		return category;
	}

	/**
	 *
	 * @param category
	 */
	public void setCategory(String category) {
		this.category = category;
	}




//	/**
//	 *
//	 * @return
//	 */
//	public List<String> getTutorialNotes() {
//		return tutorialNotes;
//	}
//
//	/**
//	 *
//	 * @param tutorialNotes
//	 */
//	public void setTutorialNotes(List<String> tutorialNotes) {
//		this.tutorialNotes = tutorialNotes;
//	}



	/**
	 *
	 * @return
	 */
	public String getKitNotes() {
		return kitNotes;
	}

	public void setKitNotes(String kitNotes) {
		this.kitNotes = kitNotes;
	}

	/**
	 *
	 * @return
	 */
	public List<String> getTutorialUrls() {
		return tutorialUrls;
	}

	/**
	 *
	 * @param tutorialUrls
	 */
	public void setTutorialUrls(List<String> tutorialUrls) {
		this.tutorialUrls = tutorialUrls;
	}

}

【问题讨论】:

  • 在此处添加引号 - "kitNotes": "Jersey RESTful Web Services..." 所有字符串值都需要引用。

标签: java json spring rest http


【解决方案1】:

您在最后一个字段 (kitNotes) 上缺少引号,因此它试图将其解析为字符串以外的其他内容:

{
 "kitName": "JERSEY Kit",
 "createdDate": "9\/7\/2015",
 "createdBy": "Reed Williams",
 "description": "JERSEY: RESTful Web Services In Java",
 "category": "jersey",
 "tutorialUrls": ["https://jersey.java.net/","https://en.wikipedia.org/wiki/Project_Jersey","http://www.mkyong.com/tutorials/jax-rs-tutorials/","https://docs.oracle.com/cd/E19776-01/820-4867/6nga7f5o5/index.html","https://www.youtube.com/watch?v=sVvL12BnIyQ","https://www.youtube.com/watch?v=_AueeTNJsyk"],
 "kitNotes": "Jersey RESTful Web Services framework is an open source, production quality framework for developing RESTful Web Services in Java that provides support for JAX-RS APIs and serves as a JAX-RS (JSR 311 & JSR 339) Reference Implementation."
}

【讨论】:

  • kitNotes 末尾需要逗号吗?如“....(JSR 311 & JSR 339)参考实现。”(这里?)
  • 如果它是对象中的最后一个元素,则不会。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-04
  • 1970-01-01
  • 1970-01-01
  • 2011-08-22
相关资源
最近更新 更多