JSON-SIMPLE Can't cast to object to read through array - java

I'm trying to use JSON-Simple to read a configuration file that's in JSON using the following code
File f = new File("config.json");
JSONParser parser = new JSONParser();
try {
int i;
StringBuilder out = new StringBuilder();
FileReader reader = new FileReader(f);
while ((i = reader.read()) != -1) {
out.append((char) i);
}
JSONArray array = (JSONArray) JSONValue.parse(out.toString());
} catch (Exception e) {
System.out.println(e.toString());
}
or
File f = new File("config.json");
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader(f));
JSONObject jsonObject = (JSONObject) obj;
JSONArray array = (JSONArray) jsonObject.get("module");
Iterator<String> itreator = array.iterator();
while (itreator.hasNext()) {
System.out.println(itreator.next());
}
} catch (FileNotFoundException fnf) {
fnf.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
but both are returning the error
org.json.simple.JSONObject cannot be cast to org.json.simple.JSONArray
however when doing
File f = new File("config.json");
try {
System.out.println(JSONValue.parse(new FileReader(f)));
} catch (Exception e) {
System.out.println(e.toString());
}
it returns the file's contents.
The config file can be seen here:
http://pastebin.com/5xJTHSwj

module tag in your config file isn't array. JSON array starts with [
e.g.
"module" : [{prop : "One"},{prop : "Two"}]
Use this code instead
JSONObject moduleObject= (JSONObject ) jsonObject.get("module");

Related

How to convert a string to JSON?

I'm using volley to get a file from the internet, the file is an array. I'm saving the file in cache to do this I have to convert the file into a string. I have a function that reads the cache and pass the response to another file to display
the information, but when i'm trying to convert the resoionse back to an array i get an error
Value AuthStatus of type java.lang.String cannot be converted to JSONObject
I'm really new on android, hoping someone can point me on the right direction
private void cacheFile(JSONObject response) {
JSONObject res = response;
String filename = "jsonfile";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(res.toString().getBytes("utf-8"));
outputStream.close();
Log.e(TAG, "Bien");
} catch (Exception e) {
e.printStackTrace();
}
}
private void readCache(String filename) {
FileInputStream inputStream;
try {
inputStream = openFileInput(filename);
inputStream.read();
String body = IOUtils.toString(inputStream, StandardCharsets.UTF_8.name());
inputStream.close();
fromCache(body);
} catch (Exception e) {
e.printStackTrace();
}
}
public void fromCache(String json) {
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(json);
JSONArray jsonArray = jsonObject.getJSONArray("cars");
Log.e(TAG, "Array Size: " + jsonArray.length());
} catch (JSONException e) {
e.printStackTrace();
}
}
You have to use JSONValue.parse method as shown below:
public void fromCache(String json) {
JSONObject jsonObject = null;
try {
jsonObject = (JSONObject)JSONValue.parse(json);
JSONArray jsonArray = jsonObject.getJSONArray("cars");
Log.e(TAG, "Array Size: " + jsonArray.length());
} catch (JSONException e) {
e.printStackTrace();
}
}

How can I read and write from and to a JSON file?

So I tried to write some code using what I found out on different sites but nothing works. Every time I try to write in to the file it doesn't work.
Here is the code for writing and reading from and in a JSON file:
HashMap<Word, ArrayList<Word>> map=null;
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("Dictionar.json"));
JSONObject jsonObject = (JSONObject) obj;
map=(HashMap) jsonObject;
} catch (Exception e) {
e.printStackTrace();
}
Dictionar dictionar=new Dictionar();
dictionar.setDictionar(map);
Here is the code for writing to the file:
JSONObject obj = new JSONObject();
obj.putAll(dictionar);
File f=new File("Dictionar.json");
f.createNewFile();
FileWriter file = new FileWriter(f);
obj.writeJSONString(file);
System.out.println("Successfully Copied JSON Object to File...");
System.out.println("\nJSON Object: " + obj);
}
catch (IOException i) {
i.printStackTrace();
}
The variable dictionar is a HashMap.

Extract value from Json using Gson

I am trying to extract values from JSON from the URL provided below using GSON java library:
http://api.wunderground.com/api/b28d047ca410515a/forecast/q/-33.912,151.013.json
I have successfully used the code provided below to extract data from URL below:
http://api.wunderground.com/api/b28d047ca410515a/conditions/q/-33.912,151.013.json
Code:
String url = "http://api.wunderground.com/api/b28d047ca410515a/conditions/q/-33.912,151.013.json";
String url2 = "http://api.wunderground.com/api/b28d047ca410515a/forecast/q/-33.912,151.013.json";
InputStream is = null;
try {
is = new URL(url).openStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JsonElement je = new JsonParser().parse(jsonText);
System.out.println("Current Temperature:" + getAtPath(je, "current_observation/temp_c").getAsString() );
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null)
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
However I am getting exception trying to extract from url2 as per code below , it seems to be a more complicated json to get values from, any help please?
// below code not working
weather_icon_url = getAtPath(je, "current_observation/icon_url").getAsString();
is = new URL(url2).openStream();
BufferedReader rd2 = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText2 = readAll(rd2);
JsonElement je2 = new JsonParser().parse(jsonText2);
System.out.println("max Temperature:" + getAtPath(je2, "forecast/simpleforecast/forecastday/high/celsius").getAsString() );
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null)
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
getAtPath code:
private static JsonElement getAtPath(JsonElement e, String path) {
JsonElement current = e;
String ss[] = path.split("/");
for (int i = 0; i < ss.length; i++) {
current = current.getAsJsonObject().get(ss[i]);
}
return current;
}
The problem you are facing is because there is an issue with the getAtPath implementation.
[{"date":{"epoch":"1459152000"... represents a JSONArray which the method is trying to access as JSONObject. Hence the IllegalStateException.
JsonObject com.google.gson.JsonElement.getAsJsonObject()
convenience method to get this element as a JsonObject. If the element
is of some other type, a IllegalStateException will result. Hence it
is best to use this method after ensuring that this element is of the
desired type by calling isJsonObject() first.
You can update and use something like below, as of now it returns only the first element.
private static JsonElement getAtPath(JsonElement e, String path) {
JsonElement current = e;
String ss[] = path.split("/");
for (int i = 0; i < ss.length; i++) {
if(current instanceof JsonObject){
current = current.getAsJsonObject().get(ss[i]);
} else if(current instanceof JsonArray){
JsonElement jsonElement = current.getAsJsonArray().get(0);
current = jsonElement.getAsJsonObject().get(ss[i]);
}
}
return current;
}
This should work:
System.out.println("max Temperature:" + getAtPath(je2, "forecast/simpleforecast/forecastday/high/celsius").getAsString() );

Is there a way to parse JSON with java? [duplicate]

This question already has answers here:
Converting JSON data to Java object
(14 answers)
Closed 6 years ago.
Here is what I have so far:
public Bitmap getAlbumCover(Context context, String song, String artist) {
this.context = context;
song = song.replace(" ", "%20");
artist = artist.replace(" ", "%20");
try {
conn = new URL("https://api.spotify.com/v1/search?q=track" + song + ":%20artist:" + artist + "&type=track)").openConnection();
} catch (IOException e) {
e.printStackTrace();
}
if (conn != null)
conn.setDoOutput(true);
try {
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
if (reader != null) {
// Read Server Response
String line2 = null;
try {
while ((line2 = reader.readLine()) != null) {
sb.append(line2);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
json = new JSONArray(sb.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
JSONParser parser= new JSONParser();
try {
JSONObject jsonObject = (JSONObject) parser.parse(reader);
try {
array = (JSONArray) jsonObject.get("items");
} catch (JSONException e) {
e.printStackTrace();
}
// take each value from the json array separately
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
The JSON I am using is located here:
https://api.spotify.com/v1/search?q=track:Ready%20To%20Fall%20artist:rise%20against%20&type=track
I am trying to get the image url located in the images array and the preview_track url located in items.
I use Jackson library to parse JSON to java opbject.
if you create your java object with the same structure as JSON this can be done using this:
ObjectMapper mapper = new ObjectMapper();
mapper.readValue(jsonUrl, YourClass.class);
So your OBJECT will have tracks and then tracks will have object album and album will have object other details. Just structure it as the JSON is and you are there.

Json object returning null against given key

I'm trying to read JSON from string (obtained from web), but it returns null.
Specifically, result.append(name + id); gives me nullnull
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(datJ);
JSONObject jsonObject = (JSONObject) obj;
String name = (String) jsonObject.get("name");
Integer id = (Integer) jsonObject.get("id");
result.append(name + id);
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (org.json.simple.parser.ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Consider that datJ contains following JSON string:
{
"rikeard":{
"id":2828822,
"name":"Rikeard",
"profileIconId":688,
"summonerLevel":30,
"revisionDate":1422917445000
}
}
EDIT: Final code working
JSONParser parser = new JSONParser();
try {
String datJ = IOUtils.toString(new URL(url));
Object obj = parser.parse(datJ);
JSONObject rikeardObject = (JSONObject) ((Map<?, ?>) obj).get("rikeard");
String name = (String) rikeardObject.get("name");
Long id = (Long) rikeardObject.get("id");
Special Thanks for Sufian and Ved!
"id" and "name" are inside the JSON object against the key "rikeard". So you need to make changes like following:
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(datJ);
JSONObject jsonObject = (JSONObject) obj;
JSONObject rikeardObject = (JSONObject) obj.get("rikeard");
String name = (String) rikeardObject.get("name");
Integer id = (Integer) rikeardObject.get("id");
result.append(name + id);
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (org.json.simple.parser.ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Use this method it's work for me,
private void extractJson(){
String jsonString="{\"rikeard\":{\"id\":2828822,\"name\":\"Rikeard\",\"profileIconId\":688,\"summonerLevel\":30,\"revisionDate\":1422917445000}}";
try {
JSONObject jsonObject=new JSONObject(jsonString);
if(jsonObject!=null){
jsonObject=jsonObject.optJSONObject("rikeard");
if(jsonObject!=null){
String id=jsonObject.optString("id");
Log.d("MainActivity","id="+id);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
Try this:
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(datJ);
JSONObject jsonObject = (JSONObject) obj;
JSONObject rikeardObject = (JSONObject) jsonObject.get("rikeard");;
String name = (String) rikeardObject .get("name");
Integer id = (Integer) rikeardObject .get("id");
result.append(name + id);
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (org.json.simple.parser.ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

Categories

Resources