Extract value from Json using Gson - java

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() );

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();
}
}

Get JSONArray and write to JSONArray

I have the following object:
{
"some_prop": "sweetvalue",
"some_list": ["0f9f822cd7e64000ac056ebc17b82f1d", "0f9f82223094fj7b82f1d"]
}
And I'm trying to get some_list and write to some_list, afterwards saving to a file. using the following code:
public String getData(String key) { // this is confusion should be getConfigValue
String data = getConfig();
JSONObject jsonData;
Object content = null;
try {
jsonData = new JSONObject(data);
if (key != null) {
content = jsonData.getString(key);
} else {
content = jsonData.toString();
}
} catch (JSONException e) {
e.printStackTrace();
}
return content.toString();
}
private void saveFile(String data) {
File file = configFile;
try (FileOutputStream fop = new FileOutputStream(file)) {
if (!file.exists()) file.createNewFile();
byte[] contentInBytes = data.getBytes();
fop.write(contentInBytes);
fop.flush();
fop.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void appendToArrayValue(String uuid) {
JSONObject configData = new JSONObject(getConfig());
JSONArray mutedPlayers = configData.getJSONArray("some_list");
JSONArray uuidArray = new JSONArray(uuid);
JSONArray newArray = concatArray(mutedPlayers, uuidArray);
JSONObject newConfigData = configData.put("some_list", newArray);
saveFile(newConfigData.toString());
}
When trying to run this code, I get the following error:
org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1]
I'm not sure what's really wrong, I know my code is terrible but that's it.
Looks like the error is at line JSONArray uuidArray = new JSONArray(uuid);?
Because prevoius line JSONArray mutedPlayers = configData.getJSONArray("some_list"); should work fine.
If so, you are passing not an array to new JSONArray(uuid) since uuid is your whole JSON object.

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-SIMPLE Can't cast to object to read through array

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");

Parsing Multiple JSON Strings to Objects using json-simple

I have a socket server running, which will emit json strings for clients. I tried to use json-simple for parsing them. But, the problem I face is that, the server doesn't have any delimiter character to segregate json strings. So, my json-simple JSONParser throws ParseException.
As an alternate, I tried to use json-smart. But, in this case, the JSONParser returns only the first object and ignores rest of the string.
I'm new to this json parsing stuff. It would be great if people can direct me to correct way of handling json string streams.
Edit: - Adding JSON String and Sample Code
{"type":"response","id":"1","result":[true,0]}{"type":"response","id":"2","result":[true,1]}
Currently this method returns the single object when I use json-smart and null when json-simple is used.
public JSONObject getResponse(JSONObject request) {
String s = null;
Socket soc = null;
PrintWriter sout = null;
BufferedReader sin = null;
try {
soc = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
sout = new PrintWriter(soc.getOutputStream());
sin = new BufferedReader(
new InputStreamReader(soc.getInputStream()));
sout.println(request.toJSONString());
sout.flush();
s = sin.readLine();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
sin.close();
sout.close();
soc.close();
} catch (Exception e) {
}
}
Object response = null;
try {
response = JSONValue.parseWithException(s.toString());
}catch (ParseException e){
e.printStackTrace();
}
return (JSONObject) response;
Thanks in advance,
Kaja
I have found a solution using Jackson. Here is the code that worked for me.
MappingJsonFactory factory = new MappingJsonFactory();
JsonParser parser = factory.createParser(soc.getInputStream());
JsonToken curToken = parser.nextToken();
if (curToken != JsonToken.START_OBJECT) {
System.err.println("Not in start object!, Exiting...");
return null;
}
while (runParser.get() == true) {
if (curToken == JsonToken.START_OBJECT) {
TreeNode node = parser.readValueAsTree();
System.out.println(node.getClass().getName());
System.out.println(node);
}
curToken = parser.nextToken();
}
Thanks Kaja Mohideen! It's working with a small change in while loop.
This code works perfectly. In my case input json is in file.
libs used : jackson-core, jackson-annotation and jackson-databind
MappingJsonFactory factory = new MappingJsonFactory();
JsonParser parser = null;
File file = new File("jsontest.txt");
try {
parser = factory.createParser(file);
JsonToken curToken = parser.nextToken();
if (curToken != JsonToken.START_OBJECT) {
System.err.println("Not in start object!, Exiting...");
}
while (parser.hasCurrentToken()) {
if (curToken == JsonToken.START_OBJECT) {
TreeNode node = parser.readValueAsTree();
System.out.println(node);
}
curToken = parser.nextToken();
}
} catch (JsonParseException e) {
e.printStackTrace();
}

Categories

Resources