json-simple

How can I cast a JSONObject to a custom Java class?

旧街凉风 提交于 2019-12-05 01:21:05
In Java ( using json-simple ) I have successfully parsed a JSON string which was created in JavaScript using JSON.stringify. It looks like this: {"teq":14567,"ver":1,"rev":1234,"cop":15678} This string is storing the state of a custom JavaScript object which I now wish to re-constitute as a pure Java class. Its not going well - first foray into Java coming from a C# background. :-p The object is currently in the form of a org.json.simple.JSONObject since that is what json-simple made from the JSONParser.parse() operation. How can I cast this JSONObject to my new Java class? (the definition of

How to tell if return is JSONObject or JSONArray with JSON-simple (Java)?

给你一囗甜甜゛ 提交于 2019-12-04 03:52:07
I am hitting a service and sometimes getting back something like this: { "param1": "value1", "param2": "value2" } and sometimes getting return like this: [{ "param1": "value1", "param2": "value2" },{ "param1": "value1", "param2": "value2" }] How do I tell which I'm getting? Both of them evaluate to a String when I do getClass() but if I try to do this: json = (JSONObject) new JSONParser().parse(result); on the second case I get an exception org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject How to avoid this? I would just like to know how to check which I'm getting back.

JSON.simple API Javadoc

血红的双手。 提交于 2019-12-03 23:33:05
Is there a Javadoc for JSON.simple? Alex Jasmin I generated the Javadoc from source and put it online . The project wiki has some interesting pages and version 1.0.2 also had an interesting README . 来源: https://stackoverflow.com/questions/2640309/json-simple-api-javadoc

Add JSON Package Reference; new to Java

心已入冬 提交于 2019-12-03 05:42:31
I'm brand new to Java and have always been a c kind-of-guy. That being said, I'm trying to use the JSON libraries (packages? classes? Java terminology is so damn confusing!) and am having issues adding them as a reference. These three imports cannot be resolved: import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.JSONValue; I went to json.org and downloaded the Java libraries but I'm not sure what to do with them. I've tried to go into project properties and add an external class to no avail. I noticed the downloaded folder is full of .java files. What

Iterate through JSONObject from root in json simple

一个人想着一个人 提交于 2019-12-03 04:52:39
问题 I am trying to iterate over a json object using json simple. I have seen answers where you can do a getJSONObject("child") from { "child": { "something": "value", "something2": "value" } } but what if I just have something { "k1":"v1", "k2":"v2", "k3":"v3" } and want to iterate over that json object. This: Iterator iter = jObj.keys(); throws: cannot find symbol symbol : method keys() location: class org.json.simple.JSONObject 回答1: Assuming your JSON object is saved in a file "simple.json",

Iterate through JSONObject from root in json simple

天大地大妈咪最大 提交于 2019-12-02 18:08:43
I am trying to iterate over a json object using json simple. I have seen answers where you can do a getJSONObject("child") from { "child": { "something": "value", "something2": "value" } } but what if I just have something { "k1":"v1", "k2":"v2", "k3":"v3" } and want to iterate over that json object. This: Iterator iter = jObj.keys(); throws: cannot find symbol symbol : method keys() location: class org.json.simple.JSONObject Assuming your JSON object is saved in a file "simple.json", you can iterate over the attribute-value pairs as follows: JSONParser parser = new JSONParser(); Object obj =

Error JSON.simple: java.util.zip.ZipException: duplicate entry: org/hamcrest/BaseDescription.class

a 夏天 提交于 2019-11-30 18:24:50
I am facing a problem in android studio after adding JSON.simple and enabling MultiDex and get the following error: Error:Execution failed for task ':app:packageAllDebugClassesForMultiDex'. java.util.zip.ZipException: duplicate entry: org/hamcrest/BaseDescription.class Here is my build.gradle : apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.1" defaultConfig { applicationId "com.MildlyGoodApps.EffortlessDescriptions" minSdkVersion 10 targetSdkVersion 23 versionCode 1 versionName "1.0" multiDexEnabled true } buildTypes { release { minifyEnabled

How to parse from JSON to Map with Json-Simple and retain key order

我是研究僧i 提交于 2019-11-30 15:47:21
I'm using Json-Simple to write a config file using JSon-Simple lib, but I'm having problems converting the json string to map. Debugging I have found that parse method returns an object that is a Map! but when I try to cast directly to a LinkedMap I get a ClassCastException: String json = aceptaDefault(); JSONParser parser = new JSONParser(); Object obj = parser.parse(json); LinkedHashMap map = (LinkedHashMap)obj; Tom McClure You can't just cast a Map to a LinkedHashMap unless you know the underlying object is actually a LinkedHashMap (or is an instance of a class that extends LinkedHashMap).

Error JSON.simple: java.util.zip.ZipException: duplicate entry: org/hamcrest/BaseDescription.class

落花浮王杯 提交于 2019-11-30 02:57:26
问题 I am facing a problem in android studio after adding JSON.simple and enabling MultiDex and get the following error: Error:Execution failed for task ':app:packageAllDebugClassesForMultiDex'. java.util.zip.ZipException: duplicate entry: org/hamcrest/BaseDescription.class Here is my build.gradle : apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.1" defaultConfig { applicationId "com.MildlyGoodApps.EffortlessDescriptions" minSdkVersion 10

Add JSON Package Reference; new to Java

我们两清 提交于 2019-11-29 12:05:06
问题 I'm brand new to Java and have always been a c kind-of-guy. That being said, I'm trying to use the JSON libraries (packages? classes? Java terminology is so damn confusing!) and am having issues adding them as a reference. These three imports cannot be resolved: import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.JSONValue; I went to json.org and downloaded the Java libraries but I'm not sure what to do with them. I've tried to go into project