Tag: json

java.lang.NoClassDefFoundError:org / json / JSONObject

我正在使用Eclipse IDE并正在编写servlet。 servlet应接受来自html文件的值并相应地返回JSON响应。 我的doPost()是: protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { try { res.setContentType(“application/json”); res.setHeader(“Cache-Control”, “nocache”); res.setCharacterEncoding(“utf-8”); PrintWriter out = res.getWriter(); JSONObject json = new JSONObject(); String un=req.getParameter(“uname”); String pw=req.getParameter(“password”); if(un.equals(“xxx”) && pw.equals(“xxx”)) json.put(“result”, “success”); else json.put(“result”, “fail”); out.print(json.toString()); } catch(Exception e){System.out.print( e.getMessage());} } 当我在Eclipse中运行这个servlet时,我得到一个文件下载对话框。 当使用Tomcat在Eclipse外部运行时,我收到错误: root cause java.lang.NoClassDefFoundError: org/json/JSONObject Server1.doPost(Server1.java:25) javax.servlet.http.HttpServlet.service(HttpServlet.java:647) […]

如何使用Jackson重命名JSON序列化中的根密钥

我正在使用Jackson进行JSON序列化对象列表。 这是我得到的: {“ArrayList”:[{“id”:1,”name”:”test name”}]} 但我想要这个: {“rootname”:[{“id”:1,”name”:”test name”}]} // ie showing the string I want as the root name. 以下是我的方法: 接口: public interface MyInterface { public long getId(); public String getName(); } 实施class: @JsonRootName(value = “rootname”) public class MyImpl implements MyInterface { private final long id; private String name; public MyImpl(final long id,final name) { this.id […]

如何在JAVA中对JSONArray进行排序

如何按对象的字段对对象的JSONArray进行排序? 输入: [ { “ID”: “135”, “Name”: “Fargo Chan” }, { “ID”: “432”, “Name”: “Aaron Luke” }, { “ID”: “252”, “Name”: “Dilip Singh” } ]; 期望的输出(按“名称”字段排序): [ { “ID”: “432”, “Name”: “Aaron Luke” }, { “ID”: “252”, “Name”: “Dilip Singh” } { “ID”: “135”, “Name”: “Fargo Chan” }, ];

JPA瞬态注释和JSON

这是关于JPA瞬态注释的以下问题的后续操作为什么JPA有@Transient注释? 我有一个我不想坚持的瞬态变量,它标有瞬态注释。 但是,当我想从我的其余控制器生成JSON时,此瞬态变量在输出的JSON中不可用。 POJO PublicationVO是直接的,没有花哨的属性,只有一些私有属性(持久化)与getter和setter以及1个瞬态变量。 @RequestMapping(value = { “{publicationId}”}, method = RequestMethod.GET, produces = “application/json”) @ResponseBody public PublicationVO getPublicationDetailsJSON(@PathVariable(value = “publicationId”) Integer publicationId) { LOG.info(“Entered getPublicationDetailsJSON – publicationId: ” + publicationId); //Call method to get the publicationVO based on publicationId PublicationVO publicationVO = publicationServices.getPublicationByIdForRestCalls(publicationId); LOG.info(“publicationVO:{}”, publicationVO); LOG.info(“Exiting getPublicationDetailsJSON”); return publicationVO; } PublicationVO如下 package com.trinity.domain.dao; import […]

反序列化时忽略属性

我有一个简单的接口,带有getter和setter属性。 public interface HasMoney { Money getMoney(); void setMoney(Money money); } 我有另一个类UserAccount,它实现了这个接口。 public class UserAccount implements HasMoney { private Money money; @Override Money getMoney() // fill in the blanks @Override void setMoney(Money money) // fill in the blanks } 我的问题是我想序列化money属性但在反序列化时忽略它,即不接受用户对此属性的任何值。 我在setter上尝试了@JsonIgnore,在getter上尝试了@JsonIgnore(false),它确实忽略了它,但它也在序列化它时也这样做了。 我在setter上尝试了@JsonIgnore,在getter上尝试了@JsonProperty,只是为了明确告诉Jackson我们打算跟踪这个属性,当money属性被发送到服务器并且Jackson试图反序化它抛出MalformedJsonException时,这似乎会崩溃应用程序:不能构造Money类型的对象。 最奇怪的是将@JsonIgnore放在setter上并且setter上的@JsonProperty适用于大多数情况下属性是原始的。

用Java解码JSON字符串

我是Java中使用json-simple库的新手,我已经完成了编码和解码示例。 复制编码示例很好,但是我无法使用混合类型的JSON来解码。 我的一个问题是库中有太多的类没有正确记录,而且我没有源(为了能够阅读并理解它们的用途)。 因此,我很难理解如何使用这些类。 看完这个例子后: String jsonText = “{\”first\”: 123, \”second\”: [4, 5, 6], \”third\”: 789}”; JSONParser parser = new JSONParser(); ContainerFactory containerFactory = new ContainerFactory(){ public List creatArrayContainer() { return new LinkedList(); } public Map createObjectContainer() { return new LinkedHashMap(); } }; try { Map json = (Map)parser.parse(jsonText, containerFactory); Iterator iter = json.entrySet().iterator(); System.out.println(“==iterate […]

Spring的Json没有得到适当的回应

我试图让Spring中的控制器返回一个JSON响应无法使用3.0推荐的Jackson类。 我当然在我的class级路径中获得了jackson jar文件(jackson-core-asl-1.5.5.jar和jackson-mapper-asl-1.5.5.jar)。 至于appconfig.xml条目,我不确定我是否需要这些。 我把它们放在那里作为绝望的最后一幕,然后回到了’时尚非json ajax’。 在调试中,我看到控制器获取请求,返回foo,然后在firebug中获得406。 错误消息如下:从记录器设置为debug时:org.springframework.web.HttpMediaTypeNotAcceptableException:找不到可接受的表示 从响应:(406)该请求标识的资源仅能够根据请求“accept”headers()生成具有不可接受特性的响应。 我的appconfig.xml在这里: 我的控制器 @RequestMapping(value=”foo/bar”, method=RequestMethod.GET) public @ResponseBody foo getFoo(@RequestParam String fooId) { return new foo(fooId); } 在jsp上,进行ajax调用: function addRow() { $.getJSON(“foo/bar”,{ fooId: 1} , function(data) { alert(“it worked.”); }); } 如果还有其他需要的信息,请告诉我。

如何在REST服务中validation传入的JSON数据?

rest服务需要根据json模式validation所有传入的json数据。 json模式是公共可访问的,可以通过http请求检索。 我正在使用jackson -framwork来编组java和json之间的编组和解组。 到目前为止,我找不到任何使用jacksonvalidation数据的方法。 我还尝试了JsonTools框架,它显然提出了这样的validationfunction。 但不幸的是,我无法让validation工作。 为什么JsonTool架构validation不起作用? 我该怎么做这样的validation?

将Jersey / Jackson配置为不使用@XmlElement字段注释进行JSON字段命名

我正在运行Jersey REST服务。 代表我的资源的POJO是JAXB(XML)带注释的简单Java类(它们是从模式定义生成的 – 因此它们具有注释)。 我希望Jersey / Jackson忽略XML-Annotations。 我在我的web.xml中做了这个配置(如这里提到的): com.sun.jersey.api.json.POJOMappingFeature true 我现在预计@ZMLElement注释不再用于JSON字段命名策略。 但是看看这个java字段(成员) @XmlElement(name = “person”, required = true) protected List persons; 我仍然得到以下JSON表示: ….,”person”:[{“name”:”FooBar”, ……. (person without the ‘s’) 所有其他字段仍然从@XmlElement注释中获取其JSON名称,而不是从Java字段名称获取。 我想实现jackson全数据绑定(POJO)示例中描述的JSON输出。 它在这样的简单测试中工作正常(使用我的XML注释类): ObjectMapper mapper = new ObjectMapper(); mapper.writeValue(System.out, myObject); 但嵌入在泽西岛我没有得到预期的JSON输出。 Jersey中的其他配置选项是否为“简单”POJO JSON表示(因为这最适合需要反序列化JSON结果的客户端)。 谢谢克劳斯 详细解决方案 (1)为Jacksons ObjectMapper实现ContextResolver ,它创建一个不使用注释的ObjectMapper。 package foo.bar.jackson; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.ext.ContextResolver; […]

如何将JSONObjects转换为JSONArray?

我有这样的回复: { “songs”:{ “2562862600”:{“id”:”2562862600″”pos”:1}, “2562862620”:{“id”:”2562862620″”pos”:1}, “2562862604”:{“id”:”2562862604″”pos”:1}, “2573433638”:{“id”:”2573433638″”pos”:1} } } 这是我的代码: List param = new ArrayList(); JSONObject json = jParser.makeHttpRequest(url, “GET”, param); JSONObject songs= json.getJSONObject(“songs”); 如何将”songs”转换为JSONArray?