Tag: json

Android 4 JSON生成错误:我可以使用比捆绑版本更新版本的org.JSON库吗?

我有一个Android应用程序,在某些地方生成JSON,串行化,然后在以后反序列化并使用数据。 我正在使用内置的JSONObject在Android 5及更高版本上查看org.json包。 我的应用程序在所有Android 5.0和更新的设备上运行良好,但在Android 4.x上它在某些地方失败了。 在调试器中查看反序列化的JSONObject看起来有点破碎。 这似乎是JSON库中的一些错误,它附带了较旧的android,我想简单地使用MavenCentral或JCenter的更新版本 我该怎么做呢? 我已经添加 compile ‘org.json:json:20160212’ 到我的应用程序的build.gradle依赖项部分,但它似乎没有任何区别。 这是可能的还是旧的破坏的android系统库总能获胜? 更新:事实certificate,这不是JSON解析中的错误,而是JSON生成中的错误。 该应用程序从Java Map和List对象生成JSON – 在Android 4中导致错误的字符串输出:更多详细信息: http : //fupeg.blogspot.co.nz/2011/07/android-json-bug.html 我通过编写以下两个函数解决了这个问题: public static JSONObject mapToJSON(Map map){ HashMap fixed = new HashMap(); for (String key : map.keySet()){ Object value = map.get(key); if (value instanceof Map){ value = mapToJSON((Map) value); } else if (value […]

ClientRequest,如何正确地将POJO序列化为json数据? 找不到内容类型application / json类型的writer:

对于REST服务(RESTeasy),我创建了JUnit测试用例,例如: @Test public void a100_insertAddressTest() throws Exception { Address addr = new Address(1, “testStreet”, “1”, (short) 1234, “testCity”); ClientRequest request = new ClientRequest(BASE_URL + “customerID/{id}”, sslExecutor_schusb); request.body(MediaType.APPLICATION_XML, addr).pathParameter( “id”, 1); ClientResponse response = request.post(String.class); Assert.assertEquals(201, response.getStatus()); response.releaseConnection(); request.clear(); } 如果我将请求正文的媒体类型更改为“application / json”,则测试用例将失败并显示错误: java.lang.RuntimeException: could not find writer for content-type application/json type: at.fhj.ase.dao.data.Address at org.jboss.resteasy.client.ClientRequest.writeRequestBody(ClientRequest.java:469) at […]

使用键作为值反序列化Jackson

我有一个类似于这样的JSON结构: “teams”: { “team1Id”: “team1Name”, “team2Id”: “team2Name” } 我想将它反序列化为这些Java类: class Teams { Team team1; Team team2; } class Team { String id; String name; } 正如您所见,team1Id和team2Id(它们是JSON密钥)应该转换为Java字段的值。 此外,第一个teamId / teamName对应该归因于存储在team1中的对象,而第二个对应存储在team2字段中。 是否有任何本地JACKSON映射器可以执行此操作,还是需要为此创建自己的自定义反序列化器?

JsonException类型java.lang.String的值stdClass无法转换为JSONObject?

在这里,我试图将json数据发送到我的web服务器并使用php我想解析json并将数据插入到mysql数据库中。 我收到一个错误,说type java.lang.String cannot be converted to JSONObject.值stdClass type java.lang.String cannot be converted to JSONObject. 我该如何解决这个错误? private void insertToDb() throws JSONException { billType = (invEstSwitch.isChecked() ? textViewEstimate : textViewInvoice) .getText().toString(); String invNumber = textViewInvNo.getText().toString(); String bcode = barCode.getText().toString(); String description = itemDesc.getText().toString(); String wt = weightLine.getText().toString(); String rateAmt = rateAmount.getText().toString(); String making = makingAmount.getText().toString(); String […]

如何使用Gson反序列化ConcurrentMap

我试图反序列化一个具有ConcurrentMap的对象,但我得到一个例外。 Caused by: java.lang.IllegalArgumentException: Can not set java.util.concurrent.ConcurrentMap field com.example.Row.doubleValues to java.util.LinkedHashMap 我的函数看起来像这样: T deserialise(final InputStream input, final Class type) { GsonBuilder gsonBuilder = new GsonBuilder(); Gson gson = gsonBuilder.create(); InputStreamReader isr = new InputStreamReader(input); return gson.fromJson(isr, type); } 如何让它正确地反序列化地图? 我需要提供自定义解串器吗? 请注意,我在同一个类中有常规Maps和ConcurrentMaps 。 我也是序列化类的人,因此,如果我可以提供一个指定并发映射类型的自定义序列化程序,然后是一个反序列化器,它指定应该如何创建那些应该工作的对象。 更新 :我反序列化的类看起来像这样: public class Row implements Serializable { private static final […]

找不到媒体类型= application / json的MessageBodyWriter

我已经尝试了一切……无法弄清楚为什么我会得到这个例外。 有趣的是,在我的IDE(Intellij)中,一切都顺利进行。 无法弄明白……放弃。 你们有什么建议吗? dependencies { compile group: ‘com.amazonaws’, name: ‘aws-java-sdk’, version: ‘1.10.27’ compile group: ‘org.slf4j’, name: ‘slf4j-api’, version: ‘1.7.7’ compile group: ‘ch.qos.logback’, name: ‘logback-classic’, version: ‘1.1.2’ compile group: ‘ch.qos.logback’, name: ‘logback-core’, version: ‘1.1.2’ compile group: ‘org.glassfish.jersey.media’, name: ‘jersey-media-json-jackson’, version: ‘2.22.1’ compile group: ‘org.glassfish.jersey.containers’, name: ‘jersey-container-grizzly2-http’, version: ‘2.22.1’ compile group: ‘org.glassfish.jersey.core’, name: ‘jersey-client’, version: ‘2.22.1’ […]

错误:Spring 3.0.3 MVC Portlet + JSON的’ContentNegotiatingViewResolver’

我想使用DispatcherPortlet类生成spring MVC 3.0.3 portlet并支持JSON。 所以,我在spring上下文文件中添加了以下配置。 text / html的 如果没有这个,如果我只使用’InternalResourceViewResolver’那么它运行正常,我可以使用portlet。 但是定义了这个bean后,我在tomcat启动时遇到了以下错误。 我google了一下,找到一个链接,声明这个带有JSON的bean只能在最新的spring vesion中使用servlet。 请检查链接。 http://jira.springframework.org/browse/SPR-7344(portlets的 JSON问题……) http://jira.springframework.org/browse/SPR-6932?page=com.atlassian.jira.plugin.system.issuetabpanels%3Aall-tabpanel#issue-tabs 另请检查下面粘贴的错误。 帮帮我……谢谢。 :错误: java.lang.IllegalArgumentException: Object of class [org.springframework.web.portlet.context.PortletRequestAttributes] must be an instance of class org.springframework.web.context.request.ServletRequestAttributes 请检查日志 Caused by: java.lang.IllegalArgumentException: Object of class [org.springframework.web.portlet.context.PortletRequestAttributes] must be an instance of class org.springframework.web.context.request.ServletRequestAttributes at org.springframework.util.Assert.isInstanceOf(Assert.java:337) at org.springframework.util.Assert.isInstanceOf(Assert.java:319) at org.springframework.web.servlet.view.ContentNegotiatingViewResolver.resolveViewName(ContentNegotiatingViewResolver.java:363) at org.springframework.web.portlet.DispatcherPortlet.resolveViewName(DispatcherPortlet.java:1110) at […]

如何在单个活动中显示多个标题和点

我正在遵循这个 SO解决方案,我已经成功parsed JSON ,但不知道如何在TextViews中显示headings和points 。 这就是我的JSON样子: { “technology”: [ { “title”: “Android”, “description”: [ { “heading”: “Manufactures”, “points”: [ “Google”, “Samsung” ] }, { “heading”: “Platforms”, “points”: [ “Kitkat”, “ICS” ] } ] } ] } 举个例子,如果我tap on Android ,那么我want to show这样的东西: Manufactures [dot] Google [dot] Samsung Platforms [dot] Kitkat [dot] ICS 供reference请看这个图像: 取代Why We […]

在HTTPServlet请求中编码UTF-8

这可能看起来像一个已经解决的问题,但事实并非如此,因为我已经完成了所有与UTF-8相关的问题,但没有一个解决方案对我有所帮助。 我正在使用JSON简单库向包含JSON对象的java servlet发送http请求。 我在Tomcat xml文件中添加了UTF-8编码 我的HTML页面支持UTF-8编码 我的数据库和我的所有表都是UTF-8编码的 我使用系统变量将JVM的默认编码更改为UTF-8(是的!这是我多么绝望) 这是我的调度员function: protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding(“UTF-8”); AjaxParser cr = AjaxParser.ClientRequestFactory(); ClientRequest msg = cr.ParseClientAjax(request); HandleRequest HR = new HandleRequest(); HandleRequestStatus HRS = HR.HandleMessage(msg); AjaxResponseGenerator ARG = new AjaxResponseGenerator(); JSONObject jsonObj = ARG.HandleResponse(HRS); response.setCharacterEncoding(“UTF-8”); response.setContentType(“application/json”); PrintWriter out = response.getWriter(); System.out.println(jsonObj);// write the […]

从YouTube API解析JSON

我有一个url: https : //gdata.youtube.com/feeds/api/users/charlieissocoollike/uploads ? alt = jsonc &v = 2 ,它提供有关用户最新YouTube上传的JSON信息。 我编写了一些代码来解析这个JSON数据,但我不明白JSON是如何工作的以及如何用Java解析它。 public void getVideoData() throws ClientProtocolException, JSONException, IOException { JSONObject object = (JSONObject) new JSONTokener(getVideoJSON().toString()).nextValue(); //String query = object.getString(“data”); JSONArray locations = object.getJSONArray(“data”); output.setText(locations.getString(1)); } public JSONObject getVideoJSON () throws ClientProtocolException, IOException, JSONException { final String URL = “https://gdata.youtube.com/feeds/api/users/charlieissocoollike/uploads?alt=jsonc&v=2”; StringBuilder url = new […]