Loop all data from json in java - java

I have a problem with Java when I try to display all of content from cmd's elements. So here is my code:
public static void main(String[] args) throws Exception {
// Json Stream Reader
String jsonS = "";
// Connect to web api
URL url = new URL("http://b50172e8.ngrok.io/api/plugin/521100d075c1284b944841394e157744");
// Make Connection
URLConnection conn = url.openConnection();
conn.setRequestProperty("Accept","*/*");
conn.connect();
// Stream reader
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
while((inputLine = in.readLine()) != null) {
jsonS+=inputLine;
}
// Read json response
Gson gson = new Gson();
// Json Object
JsonObject jsonObject= gson.fromJson(jsonS, JsonObject.class);
JsonElement data = jsonObject.get("data");
System.out.println(data);
// Close connection
in.close();
}
Output:
[{"cmd":"cmd-1"},{"cmd":"cmd-2"},{"cmd":"cmd-3"}]
I want to use foreach of cmd to display the following:
cmd-1
cmd-2
cmd-3

Try this code. I hope it helps.
public static void main(String[] args) throws Exception {
// Json Stream Reader
String jsonS = "";
// Connect to web api
URL url = new URL("http://b50172e8.ngrok.io/api/plugin/521100d075c1284b944841394e157744");
// Make Connection
URLConnection conn = url.openConnection();
conn.setRequestProperty("Accept","*/*");
conn.connect();
// Stream reader
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
while((inputLine = in.readLine()) != null) {
jsonS+=inputLine;
}
// Read json response
Gson gson = new Gson();
// Json Object
JsonObject jsonObject= gson.fromJson(jsonS, JsonObject.class);
JsonArray data = jsonObject.getAsJsonArray("data");
//here data is JsonArray and it contains everithing: [{"cmd":"cmd-1"},{"cmd":"cmd-1"},{"cmd":"cmd-1"}]
data.forEach(el -> {
//Get Json object which has key and value -> {"cmd":"cmd-1"}
JsonObject jo = el.getAsJsonObject();
//get the value as Json element -> "cmd-1"
JsonElement je = jo.get("cmd");
//Then make the json element string
String value = je.getAsString();
System.out.println(value);
});
//System.out.println(data);
// Close connection
in.close();
}

Related

Parsing JSON Object from a JSON array

I am currently developing an app and need to parse JSON objects from inside an unnamed array.
I can only manage to parse JSON arrays with a name such as this one: http://jsonparsing.parseapp.com/jsonData/moviesDemoItem.txt.
The code that I used for the one above is
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String asd = buffer.toString();
JSONObject parentObject = new JSONObject(asd);
JSONArray parentArray = parentObject.getJSONArray("movies");
JSONObject fObject = parentArray.getJSONObject(0);
String movie = fObject.getString("movie");
int year = fObject.getInt("year");
return movie + year;
The code includes "movies" which is the array name .
What should I change to parse only the objects from within a JSON array such as https://restcountries.eu/rest/v1/all?
Your countries list is simply an array. Doesn't need a name.
Simply replace
JSONObject parentObject = new JSONObject(asd);
with
JSONArray parentObject = new JSONArray(asd);
See this post for how to iterate over that array to parse the remainder of the objects.
How to parse JSON in Android
Starting something like
for (int i=0; i < parentObject.length(); i++) {
Alternatively, Volley's JsonArrayRequest would be useful, or learning about Retrofit+Gson would be even better if you don't feel like manually parsing the JSON data yourself.

Parse device data with simple json and building a comma delimited file in hdfs

My question is around how to parse a json response from a device's api. The json looks like below.
[{"name":"Device 1","label":"Switch
State","value":"off"},{"name":"Device 2","label":"Switch
State","value":"on"}]
public class Fubar{
public static void main(String[] args) throws Exception {
boolean a = true;
while (a) {
Configuration conf = new Configuration();
FileSystem hdfs = FileSystem.get(conf);
String fileName = "filethingy" + "-" + new Date().getTime() + ".txt";
boolean b = true;
while(b){
String connString = "url";
URL url = new URL(connString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
out.writeBytes("TimeStamp"+",");
out.writeBytes("DeviceName"+",");
out.writeBytes("DeviceLabel"+",");
out.writeBytes("DeviceStatus"+",");
out.writeBytes("\n");
while ((inputLine = in.readLine()) != null) {
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(inputLine);
String date = new java.sql.Timestamp(System.currentTimeMillis()).toString();
out.write(date.getBytes());
out.write(",".getBytes());
out.writeBytes((String) jsonObject.get("name"));
out.write(",".getBytes());
out.writeBytes((String) jsonObject.get("label"));
out.write(",".getBytes());
out.writeBytes((String) jsonObject.get("value"));
//out.write(inputLine.getBytes());
out.write("\n".getBytes());
out.hflush();
}
in.close();
Thread.sleep(30000);
out.hflush();
}
out.close();
hdfs.close();
a=false;
b=false;
}
This doesn't work I know because I can't convert a JSONArray to a JSONObject. I'm having trouble figuring out how to pull apart the JSON correctly though to build ultimately a csv that looks like TimeStamp,DeviceName, DeviceLabel, DeviceStatus, 01012001, Device1, Switch Status, On. Someone's going to put a Hive table on top of the data when we're done. Any help would be appreciated.
Answered my own question.
while ((inputLine = in.readLine()) != null) {
JSONParser jsonParser = new JSONParser();
JSONArray jsonArray = (JSONArray) jsonParser.parse(inputLine);
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject object = (JSONObject) jsonArray.get(i);
String date = new java.sql.Timestamp(System.currentTimeMillis()).toString();
out.write(date.getBytes());
out.write(",".getBytes());
out.writeBytes((String) object.get("name"));
out.write(",".getBytes());
out.writeBytes((String) object.get("label"));
out.write(",".getBytes());
out.writeBytes(String.valueOf(object.get("value")));
//out.write(inputLine.getBytes());
out.write("\n".getBytes());
out.hflush();
}

create json object from request body which contains JSON data

My request body contains JSON. i have to read that JSON save it as JSON object. And i don't have any pojo class representing the data in json. i tried this , and this and i am using com.ibm.json.java.JSONObject.i have tried this ,
BufferedReader ne = req.getReader();
StringBuffer jb = new StringBuffer();
try {
BufferedReader reader = req.getReader();
while ((line = reader.readLine()) != null)
jb.append(line);
} catch (Exception e) { /*report an error*/ }
JSONObject jsonObject = new JSONObject();
jsonObject.put("data", jb.toString());
above implementation is dumb because i am converting request header to string and adding it to jsonobject.
I think you should use public static JSONObject parse( java.lang.String str) in class JSONObject .Also there is no need to convert Reader to String as there are overloaded methods in JSONObject which take Inputstream and Reader eg public static JSONObject parse( java.io.Reader reader)

Can't properly parse JSON

I am having some issues while parsing JSON response in Android. The response I get is:
{
"response": "{\"session_token\":\"48500d8e42acc09aa45cb8f3a7ba2b30\",\"user_login\":\"newoff2\",\"user_id\":\"62\",\"user_profile_img\":\"http://onepgr.com/system/photos/62/medium/userfile054c35e29.png?1422089771\",\"success\":\"0\",\"user_email\":\"newoff2#pdmoffice.com\"}"
}
I need the values for user_login, success, user_profile_img, user_email. Here is what I tried so far, but it won't do what I need:
HttpResponse response = httpClient.execute(httpPost);
// write response to log
Log.d("Http Post Response:", response.toString());
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String json = reader.readLine();
Log.d("Final Response",json);
jsonObject = new JSONObject(json);
JSONObject json1=jsonObject.getJSONObject("response");
String str = json1.getString("success");
Log.e("Parsed data is",str);
use this
json=json.replace("\\\"", "\"");
Log.e("resule",json);
try {
JSONObject jsonObject = new JSONObject(json);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
you can use regex to make your string JSON parsable
var res = data.replace(/\\"/g, '').replace(/\"{/g, '{').replace(/\}"/g, '}');
var jsonData = JSON.parse(res);
alert(jsonData.response.user_login);
Here is FIDDLE
Note: in fiddle I have declared your JSON with a ' ' to make it complete string
Use Scanner to remove \:
String resultStr = new Scanner(json).useDelimiter("\\A").next();
jsonObject = new JSONObject(resultStr);
Above is used for BufferedInputStream to get JSON string.
[UPDATE:]
For BufferReader, need to use StringBuilder to get JSON string:
StringBuilder strBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
strBuilder.append(line);
}
//for your JSON string, should use 'JSONTokener' to parse
jsonObject = (JSONObject) new JSONTokener(strBuilder.toString()).nextValue();
JSONObject json1=jsonObject.getJSONObject("response");
String str = json1.getString("success");
This should work for your case!
Try this....
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
Log.d("Result",result);
JSONObject jsonObject = new JSONObject(result);
String resJson=jsonObject.getString("response");
Log.d("Result",resJson);
JSONObject jsparam=new JSONObject(resJson);
String success=jsparam.getString("success");
Log.d("Value for success",success);
// JSONObject json1=jsonObject.getJSONObject("response");
//String objResponse = json1.getString("success");
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}

How to adjust the number of results returned when querying the Stackoverflow API?

I'm using the Stackoverflow JSON API to retrieve questions marked with a given tag.
I have this small program in Java which retrieves questions marked with the "Java" tag.
public static void main(String[] args) throws Exception
{
String urlString = "https://api.stackexchange.com/2.1/questions?order=desc&sort=votes&tagged=java&site=stackoverflow";
URL url = new URL( urlString );
BufferedReader reader = null;
StringBuffer buffer = new StringBuffer();
try
{
URLConnection connection = url.openConnection();
InputStream isConn = connection.getInputStream();
reader = new BufferedReader( new InputStreamReader( new GZIPInputStream( isConn ) ) );
String inputLine;
while (( inputLine = reader.readLine() ) != null)
{
buffer.append( inputLine );
}
}
finally
{
if (reader != null)
{
reader.close();
}
}
JSONObject jsonObject = new JSONObject( buffer.toString() );
JSONArray jsonArray = jsonObject.getJSONArray( "items" );
System.out.println( buffer );
System.out.println( jsonArray.length() );
}
My problem is that it returns only 30 questions. Since my goal is to build a dataset for further textual analysis, I need to access way more than just 30 questions.
Is there a way to adjust the size of the returned list?
If not, how can I workaround this situation?
Notice the has_more property in the returned JSON, this indicates that more results are available. You can page through these results using the page and pagesize parameters in the url. The issue I foresee is the code will be pulling a large number of questions considering it will iterate through all java questions, so you may want to add a conditional that stops at a certain number of pages. Here is a quick example:
public static void main(String[] args) throws Exception {
BufferedReader reader = null;
int page = 1;
JSONObject jsonObject = null;
try {
while (jsonObject == null || jsonObject.getBoolean("has_more")) {
String urlString = "https://api.stackexchange.com/2.1/questions?order=desc&sort=votes&tagged=java&site=stackoverflow&pagesize=100";
urlString += "&page=" + page++;
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
InputStream isConn = connection.getInputStream();
StringBuffer buffer = new StringBuffer();
reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(isConn)));
String inputLine;
while ((inputLine = reader.readLine()) != null) {
buffer.append(inputLine);
}
jsonObject = new JSONObject(buffer.toString());
JSONArray jsonArray = jsonObject.getJSONArray("items");
System.out.println(buffer);
System.out.println(jsonArray.length());
}
} finally {
if (reader != null) {
reader.close();
}
}
}

Categories