My program is to get a list of Java objects from the "result" string derived from the AsyncTask. In order to do so, I need to use JsonParser to get an JsonArray. In case the "result" string is [], the JsonArray is also []. How can I detect if there is any item in this Json array. I've tried all of suggestions, even detected at the "result" string, nothing is working for me, could you please help? My JsonArray: []
try{
JsonParser parser = new JsonParser();
JsonArray array = parser.parse(orderList).getAsJsonArray();
System.out.println("Inside fromJasonToJava, array: " + array); //This prints out array: []
lst = new ArrayList<Order>();
if(array.get(0)!= null) { //It goes wrong here, I need a working "if" condition
//Value is not null
for (final JsonElement json : array) {
JSONObject jsonObject = new JSONObject(String.valueOf(json));
System.out.println("Inside fromJasonToJava, jsonObject:" + jsonObject);
}
}
}catch(Exception e){
throw new Exception("Convert json to java error [fromJasonToJava()]", e);
}
Inside your if check for size instead, if you are using org.json.simple.JSONArray
if(array.size() != 0)
or if you using org.json.JSONArray
if(array.length() != 0)
You may use if(array.length() > 0)
You no need to use the if condition as java's for-each is smart enough to handle this.
If the array doesn't have any vale or it's empty then it will not get executed at all.
So use this code:
for (final JsonElement json : array) { // it will not get executed at all if array is empty
JSONObject jsonObject = new JSONObject(String.valueOf(json));
System.out.println("Inside fromJasonToJava, jsonObject:" + jsonObject);
Instead of
if(array.get(0)!= null) { //It goes wrong here, I need a working "if" condition
//Value is not null
for (final JsonElement json : array) {
JSONObject jsonObject = new JSONObject(String.valueOf(json));
System.out.println("Inside fromJasonToJava, jsonObject:" + jsonObject);
}
Related
I need to write a code which would convert JSON file to CSV. The problem is in a format that the CSV file should look like.
Input json:
{
"strings":{
"1level1":{
"1level2":{
"1label1":"1value1",
"1label2":"1value2"
}
},
"2level1":{
"2level2":{
"2level3":{
"2label1":"2value1"
},
"2label2":"2value2"
}
}
}
}
And this is expected csv file for this json:
Keys,Default
1level1.1level2.1label1,1value1
1level1.1level2.1label2,1value2
2level1.2level2.2level3.2label1,2value1
2level1.2level2.2label2,2value2
I was trying to go through JSON file using recursion but this didn't work for me because of rewriting JSON object on each iteration and code was working only till the first value. Are there any suggestions about how can it be done?
Note: have tried to use different JSON libraries, so for now can be used any of them
UPDATE #1:
Non-working code example I was trying to use to go through JSON tree:
public static void jsonToCsv() throws JSONException {
InputStream is = MainClass.class.getResourceAsStream("/fromJson.json");
JSONTokener jsonTokener = new JSONTokener(is);
JSONObject jsonObject = new JSONObject(jsonTokener);
stepInto(jsonObject);
}
private static void stepInto(JSONObject jsonObject) {
JSONObject object = jsonObject;
try {
Set < String > keySet = object.keySet();
for (String key: keySet) {
object = object.getJSONObject(key);
stepInto(object);
}
} catch (JSONException e) {
Set < String > keySet = object.keySet();
for (String key: keySet) {
System.out.println(object.get(key));
}
e.printStackTrace();
}
}
UPDATE #2:
Another issue is that I will never know the names of the JSON object and count of child objects (update JSON and CSV examples as well to make the image more clear). All that is known, that it will always start with strings object.
Library used:
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>
So found a solution by myself:
public static void jsonToCsv() throws JSONException, IOException {
InputStream is = MainClass.class.getResourceAsStream("/fromJson.json");
JSONTokener jsonTokener = new JSONTokener(is);
JSONObject jsonObject = new JSONObject(jsonTokener).getJSONObject("strings");
builder = new StringBuilder();
while (!jsonObject.isEmpty()) {
stepInto(jsonObject);
}
String[] lines = builder.toString().split("\n"); // builder lines are in reverse order from expected so this array is used to reverse them
FileWriter csvWriter = new FileWriter("src/main/resources/toCsv.csv");
csvWriter.append("Keys,Default (en_US)\n");
for (int i = lines.length - 1; i >= 0; i--) {
csvWriter.append(lines[i]).append("\n");
}
csvWriter.flush();
csvWriter.close();
}
private static void stepInto(JSONObject jsonObject) {
for (String key: jsonObject.keySet()) {
Object object = jsonObject.get(key);
if (object instanceof JSONObject) {
builder.append(key).append(".");
stepInto(jsonObject.getJSONObject(key));
} else {
builder.append(key).append(",").append(object).append("\n");
jsonObject.remove(key);
break;
}
if (jsonObject.getJSONObject(key).isEmpty()) {
jsonObject.remove(key);
}
break;
}
}
I think you just missed keeping track of your result, otherwise it looks good.
Let's say your result is a simple string. Then you have to concatenate all keys while traversing the json object until you reach a primitive value (like a number or a string).
(I am writing this out of my head, so please forgive me for incorrect syntax)
private static String stepInto(JSONObject jsonObject) { // we change "void" to "String" so we can record the results of each recursive "stepInto" call
//JSONObject object = jsonObject; // we don't need that. Both variables are the same object
String result ="";
try {
for (String key: jsonObject.keySet()) { // shorter version
Object object = jsonObject.get(key); // Attention! we get a simple Java Object
if(object instanceof JSONObject){
result+= key+"."+stepInto(jsonObject.getJSONObject(key)); // the recursive call, returning all keys concatenated to "level1.level2.level3" until we reach a primitive value
}
if(object instanceof JSONArray){
result+= key+", "+ ... // notice how we use the csv separator (comma) here, because we reached a value. For you to decide how you want to represent arrays
}
result+= key +", "+ object +"\n"; // here I am not sure. It may well be that you need to check if object is a String an Integer, Float or anything.
}
} catch (JSONException e) {
for (String key: jsonObject.keySet()) {
System.out.println(object.get(key));
}
e.printStackTrace();
result+= "\n"; // I added this fallback so your program can terminate even when an error occurs.
}
return result; // sorry, I forgot to accumulate all results and return them. So now we have only one actual "return" statement that will terminate the call and return all results.
}
As you can see, I didn't change much of your original method. The only difference is that now we keep track of the keys ("level1.level2...") for each recursive call.
EDIT
I added a +"\n"; so everytime we reach a value so we can terminate that "line".
AND more importantly, instead of returning everytime, I add the result of each call to a string, so we continue looping over the keys and concatenate all results. Each call of the method will return only once all keys are looped over. (sorry that missed that)
In your calling method you could print out the result, something like that:
JSONObject jsonObject = new JSONObject(jsonTokener);
String result = stepInto(jsonObject);
System.out.println(result);
I am receiving json and am trying to operate on it using the JSONObject and JSONArray classes. I have nested objects and need to determine if those nested objects are arrays or objects. The problem is that if I assume it is an object and it is actually an array, when I get call getJSONObject, an exception is raised. I'm wondering if the only way to do this is just to add try/catch blocks or if there is a better way to do this. For example, say I have:
{"key1": "val1",
"key2": {"level2": {"level3": "val3"}}
}
I may also have the level2 element with multiple values and coming in as an array:
{"key1": "val1",
"key2": {"level2": [{"level3": "val3"}]}
}
How would I extract the level2 object (as an object) and determine if it is an object or an array? Is the only way to really do this using string manipulation?
If I do something like:
jsonObj.getJSONObject("key2").getJSONObject("level2");
It will result in an error: "org.json.JSONException: JSONObject["level2"] is not a JSONObject and conversly, if I call getJSONArray("level2") and level2 is not an array I will get an error indicating it is not an array.
You can use optJSONObject() & optJSONArray() methods to check if the underlying is an Object or Array. Below is an example for that.
JSONObject json = new JSONObject("{\"key1\": \"val1\",\n" +
" \"key2\": {\"level2\": {\"level3\": \"val3\"}}\n" +
"}");
JSONObject json1 = new JSONObject("{\"key1\": \"val1\",\n" +
" \"key2\": {\"level2\": [{\"level3\": \"val3\"}]}\n" +
"}");
if(json.getJSONObject("key2").optJSONObject("level2") != null) {
System.out.println("json : it is a jsonobject");
}
else if(json.getJSONObject("key2").optJSONArray("level2") != null) {
System.out.println("json : it is a jsonarray");
}
if(json1.getJSONObject("key2").optJSONObject("level2") != null) {
System.out.println("json1 : it is a jsonobject");
}
else if(json1.getJSONObject("key2").optJSONArray("level2") != null) {
System.out.println("json1 : it is a jsonarray");
}
Use 'jsonObj.get("level2)' and check whether the type is JsonArray and then try to cast it.
If you're open to using Jackson's Databind module, it's pretty straightforward:
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
// ...
// parse your JSON data into a JsonNode
JsonNode jsonObj = new ObjectMapper().readTree(yourJsonString);
// should really do some error checking here unless you're 100% sure
// you're always going to be getting "key2" and "level2" in your JSON
// data structure; look into Jackson's "hasNonNull()" function
JsonNode level2 = jsonObj.get("key2").get("level2");
if (level2.isArray()) {
ArrayNode level2arr = (ArrayNode)level2;
// ... do something with level2arr as an array
// ...
} else if (level2.isObject()) {
// ... use it as an object node
// ...
}
I would stringify the JSON object and check the index of '{' or '['.
Given a Json, I would stringify it. And if the index of '{' is 0 in the stringified object then its a Json Object and if the index of '[' is 0 in the stringified object then its a Json Array.
I use Vertx framework and I can verify them as follows:
JsonObject jsonObject = new JsonObject().put("name", "avishek");
JsonArray jsonArray = new JsonArray().add("Vizury").add("MediaIQ");
System.out.println(jsonObject.toString().indexOf("{") == 0); //Json Object
System.out.println(jsonArray.toString().indexOf("[") == 0); //Json Array
I am parsing JSON data from this link:
http://twyst.in/api/v1/near/30/30
As the data is too large, I am posting only the (problem) relevant data here:
{
"info": "[{\"outlet\":{\"__v\":1,\"_id\":\"5316d59326b019ee59000026\",\"photos\":[],\"twyst_meta\":{\"recommend_list\":[],\"reviews\":[]},\"outlet_meta\":{\"links\":[],\"status\":\"active\",\"accounts\":[\"531574eeae738d654c00000a\",\"535a1a1e827651ca03000122\"]},\"attributes\":{\"dine_in\":true,\"outdoor\":true,\"air_conditioning\":\"Not Available\",\"parking\":\"Available\",\"reservation\":\"Not Required\",\"wifi\":\"Not Available\",\"tags\":[\"Pizza\",\"Pasta\",\"Italian\",\"Galleria\",\"DLF Phase 4\"],\"payment_options\":[\"cash\",\"amex\",\"visa\",\"master\"],\"cuisines\":[\"Italian\",\"Pizza\",\"Gelato\"],\"timings\":\"11 AM - 11 PM\",\"cost_for_two\":{\"min\":3,\"max\":4}},\"links\":{\"other_urls\":[],\"youtube_url\":\"\",\"twitter_url\":\"\",\"facebook_url\":\"https://www.facebook.com/pages/Crusty-gourmet-pizza-more/390843954384130\",\"website_url\":\"\"},\"contact\":{\"emails\":{\"email\":\"bob_nugent56#yahoo.com\",\"person\":\"\"},\"phones\":{\"number\":\"\",\"landline\":\"02355156616\",\"reg_mobile\":[{\"_id\":\"4639320000e4532779b17de7\",\"num\":\"9134000410\"}
This is not the end of JSON data, it goes on...
But when the following line of code is executed, I am getting a JSONException. The e.printStackTrace() method tells me that there is typeMismatch error.
JSONObject object = new JSONObject(str); //1
JSONArray array = object.getJSONArray("info"); //2
I have tried replacing the line 2 with the following code:
JSONObject newObject = object.getJSONObject("info");
But here also I encountered the same problem.
I have checked the complete data, it is correct, no syntax error.
Can anybody help me with the issue ? Can the presence of double quotes before starting of JSONArray be the root of this problem ?
Thanks.
info is not an array or an object. It's a string, which just happens to contain the JSON-encoded representation of another array. You will have to get that string and run it through a second JSON decoding pass:
JSONArray array = new JSONArray(object.getString("info"));
JSONObject json;
Object info;
JSONArray infoJsonArray;
JSONObject infoObject;
json = new JSONObject(str);
Object info= json.get("info");
if (info instanceof JSONArray) {
// It's an array
infoJsonArray = (JSONArray)info;
}
else if (info instanceof JSONObject) {
// It's an object
infoObject = (JSONObject)info;
} else {
// It's something else, like a string or number
}
Hello i call a service if it contains multiple objects it make list but when it contain only one object it return a single object not a list [] are missing , actually i want to convert them into java class using gson but in case of single exception it throw exception but when it contain list it work fine i actually need to convert my single gSON string to array ,please help me ..here is the string
{
"response":{
"projects":{
"project":{
"ixWorkflow":1,
"sEmail":"j.a#loxvo.com",
"sPhone":"",
"ixProject":2,
"ixPersonOwner":2,
"fDeleted":false,
"sProject":"Project Default",
"fInbox":true,
"sPersonOwner":"junaid"
}
}
}
}
i want it to be like same as
{
"response":{
"projects":{
"project":[
{
"ixWorkflow":1,
"sEmail":"j.a#loxvo.com",
"sPhone":"",
"ixProject":6,
"ixPersonOwner":2,
"fDeleted":false,
"sProject":"project 2",
"fInbox":false,
"sPersonOwner":"junaid"
},
{
"ixWorkflow":1,
"sEmail":"j.a#loxvo.com",
"sPhone":"",
"ixProject":2,
"ixPersonOwner":2,
"fDeleted":false,
"sProject":"Project Default",
"fInbox":true,
"sPersonOwner":"junaid"
}
]
}
}
}
With reference to https://stackoverflow.com/a/7284813/1105291
Please try below code before you pass json to Gson for object conversion, and please let me know if you get any error. Only posibility that I can see is exception at if.
JSONObject jsonObject = new JSONObject(responseString);
JSONObject projectsJsonObject = jsonObject.getJSONObject("response").getJSONObject("projects");
if(projectsJsonObject.getJSONArray("project") == null)
{
JSONArray jsonArray = new JSONArray();
jsonArray.put(projectsJsonObject.getJSONObject("project"));
projectsJsonObject.put("project", jsonArray);
}
//Pass jsonObject to Gson
Use Google Gson
JsonParser parser = new JsonParser();
JsonObject o = (JsonObject)parser.parse("{\"a\": \"A\"}");
I have a json stream which can be something like :
{"intervention":
{
"id":"3",
"subject":"dddd",
"details":"dddd",
"beginDate":"2012-03-08T00:00:00+01:00",
"endDate":"2012-03-18T00:00:00+01:00",
"campus":
{
"id":"2",
"name":"paris"
}
}
}
or something like
{"intervention":
[{
"id":"1",
"subject":"android",
"details":"test",
"beginDate":"2012-03-26T00:00:00+02:00",
"endDate":"2012-04-09T00:00:00+02:00",
"campus":{
"id":"1",
"name":"lille"
}
},
{
"id":"2",
"subject":"lozlzozlo",
"details":"xxx",
"beginDate":"2012-03-14T00:00:00+01:00",
"endDate":"2012-03-18T00:00:00+01:00",
"campus":{
"id":"1",
"name":"lille"
}
}]
}
In my Java code I do the following:
JSONObject json = RestManager.getJSONfromURL(myuri); // retrieve the entire json stream
JSONArray interventionJsonArray = json.getJSONArray("intervention");
In the first case, the above doesn't work because there is only one element in the stream..
How do I check if the stream is an object or an array ?
I tried with json.length() but it didn't work..
Thanks
Something like this should do it:
JSONObject json;
Object intervention;
JSONArray interventionJsonArray;
JSONObject interventionObject;
json = RestManager.getJSONfromURL(myuri); // retrieve the entire json stream
Object intervention = json.get("intervention");
if (intervention instanceof JSONArray) {
// It's an array
interventionJsonArray = (JSONArray)intervention;
}
else if (intervention instanceof JSONObject) {
// It's an object
interventionObject = (JSONObject)intervention;
}
else {
// It's something else, like a string or number
}
This has the advantage of getting the property value from the main JSONObject just once. Since getting the property value involves walking a hash tree or similar, that's useful for performance (for what it's worth).
Maybe a check like this?
JSONObject intervention = json.optJSONObject("intervention");
This returns a JSONObject or null if the intervention object is not a JSON object. Next, do this:
JSONArray interventions;
if(intervention == null)
interventions=jsonObject.optJSONArray("intervention");
This will return you an array if it's a valid JSONArray or else it will give null.
To make it simple, you can just check first string from server result.
String result = EntityUtils.toString(httpResponse.getEntity()); //this function produce JSON
String firstChar = String.valueOf(result.charAt(0));
if (firstChar.equalsIgnoreCase("[")) {
//json array
}else{
//json object
}
This trick is just based on String of JSON format {foo : "bar"} (object)
or [ {foo : "bar"}, {foo: "bar2"} ] (array)
You can get the Object of the input string by using below code.
String data = "{ ... }";
Object json = new JSONTokener(data).nextValue();
if (json instanceof JSONObject)
//do something for JSONObject
else if (json instanceof JSONArray)
//do something for JSONArray
Link: https://developer.android.com/reference/org/json/JSONTokener#nextValue
Object valueObj = uiJSON.get(keyValue);
if (valueObj instanceof JSONObject) {
this.parseJSON((JSONObject) valueObj);
} else if (valueObj instanceof JSONArray) {
this.parseJSONArray((JSONArray) valueObj);
} else if(keyValue.equalsIgnoreCase("type")) {
this.addFlagKey((String) valueObj);
}
// ITERATE JSONARRAY
private void parseJSONArray(JSONArray jsonArray) throws JSONException {
for (Iterator iterator = jsonArray.iterator(); iterator.hasNext();) {
JSONObject object = (JSONObject) iterator.next();
this.parseJSON(object);
}
}
I haven't tryied it, but maybe...
JsonObject jRoot = RestManager.getJSONfromURL(myuri); // retrieve the entire json stream
JsonElement interventionElement = jRoot.get("intervention");
JsonArray interventionList = new JsonArray();
if(interventionElement.isJsonArray()) interventionList.addAll(interventionElement.getAsJsonArray());
else interventionList.add(interventionElement);
If it's a JsonArray object, just use getAsJsonArray() to cast it. If not, it's a single element so just add it.
Anyway, your first exemple is broken, you should ask server's owner to fix it. A JSON data structure must be consistent. It's not just because sometime intervention comes with only 1 element that it doesn't need to be an array. If it has only 1 element, it will be an array of only 1 element, but still must be an array, so that clients can parse it using always the same schema.
//returns boolean as true if it is JSONObject else returns boolean false
public static boolean returnBooleanBasedOnJsonObject(Object jsonVal){
boolean h = false;
try {
JSONObject j1=(JSONObject)jsonVal;
h=true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
if(e.toString().contains("org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject")){
h=false;
}
}
return h;
}