For Loop to iterate over JSON objects inside an Array - java

How do I get and show each and every JSON's Object's value those I have inside an Array called learning
Here is how JSON Array looks like:
"learning": [
{
"code":"2K14 - 2",
"os":"Windows - 2"
},
{
"code":"2K15 - 2",
"os":"Linux - 2"
},
{
"code":"2K16 - 2",
"os":"Mac - 2"
}
]
Code
List<Learning> learning = value.getLearning();
for(Learning m : learning) {
// I guess, here I am missing something, which is really useful
String code = m.getCode();
String os = m.getOs();
viewHolder.learning.setText("Code: "+code+" OS: "+os);
}
When I execute my program, getting this:
Code: 2k16, OS: Mac - 2
Whereas I want to get something like this:
Code: 2k14 OS: Windows - 2, Code: 2k15 OS: Linux - 2, Code: 2k16 OS: Mac - 2

Do it as, if want to show all data in single TextView:
viewHolder.learning.append("Code: "+code+" OS: "+os + ", ");
Use TextView.append instead of TextView.setText

Can you post your Retrofit interface and all the model classes related to that?
and then you will have something like this to read it.
String text = "";
List<Learning> learning = value.getLearning();
for(Learning m : learning) {
// I guess, here I am missing something, which is really useful
String code = m.getCode();
String os = m.getOs();
text = text + "Code: " + code + " OS: " + os + ", ";
}
viewHolder.learning.setText(text);

JSONArray jsonArray = jsonObj.getJSONArray("learning");
for(int i = 0; i < jsonArray.length(); i++)
{
JSONObject obj = jsonArray.getJSONObject(i);
String code = obj.getString("code");
String os = obj.getString("os");
Log.i("MyClass", "Code -> " + code);
Log.i("MyClass", OS -> " + os);
}

Related

How can I retrieve qrcode data from an object?

I have a method that can return QRcode data in {'p':1,"x":10,"y":20} this format and it is captured in a variable called qrdata (which is a String data type ).
My question is how can I retrieve value from that key , value pair object ?if .... String qrdata = readQR(image) then how can I retrieve data from qrdata ?
You can use the JSONObject class from javax.json as mentioned in BuildSlayer's answer.
In the example in your question you use simple quotes for 'p'. Assuming it is not a typo and you meant that different charcacters can be used as quotes, you could run into trouble using it though. In that case, you could use plain java to parse your qrData String:
public String[] parseJSON(String qrData){
String[] output = new String[qrData.length()];
int i = 0;
for (String keyValuePair:qrData.split(",")) {
output[i++] = keyValuePair.split(":")[1];
}
return output;
}
}
You can do the following:
JSONObject jsonObject = new JSONObject(qrData);
System.out.println("p: " + jsonObject.getInt("p"));
System.out.println("x: " + jsonObject.getInt("x"));
System.out.println("y: " + jsonObject.getInt("y"));
Output:
p: 1
x: 10
y: 20

LDAP with Jacob-ADO-Wrapper

after a little bit of trying I managed to get results from the ldap-server at my company. Now I have a little problem and I seem to be too dump to find any documentation about it.
Command objCmd = new Command();
Recordset RS = new Recordset();
objCmd.setActiveConnection(conn);
objCmd.setCommandText("<LDAP://scdldap.siemens.net:389>;(&(objectClass=scdInternetPerson)(mail=" + email + "));" +searchKeyword+";subTree");
RS = objCmd.Execute();
if (RS.getBOF())
System.out.printf(email + ";" + "null" + "\n");
else {
RS.MoveFirst();
System.out.printf(email + ";" + RS.getFields().getItem(0).getValue() + "\n");
}
This works fine as long as I print the result out to the console. But I would need to get the value as a String (it is always a String), but I can't make it. Can somebody tell me what I am missing? I know this is some VariableType Error, because the result is of type Variant, but
Variant.toString() or anything else is not possible.
Try to convert Variant to Object and then to String, for example:
Variant From = new Variant(1);
Variant To = new Variant(6);
Object[] args = new Object[]{From, To};
String From1 = args[0].toString();
String To1 = args[1].toString();

Java parser time issue

I am writing a Java parser that get ASANA APP data from ASANA server and parse it and then save it to local DB for customized reports. I have issue with the time needed to execute this Java utility.
I have this sub part of the Java parser. When I run this, it takes approximately 10 seconds to execute. If this code executes in 10 sec than one line this is given below:
JSONObject Project_jsonObject = (JSONObject) Project_jsonParser.parse(Project_br);
This line execute in 9 seconds out of total 10 seconds. How I can reduce this time. Any technique or tip or alternate to reduce time.Please help me I am waiting
static File workDir = new File("C:/cygwin64/bin");
static Runtime systemShell = Runtime.getRuntime();
String project_request="curl -u 4cKDgv4O.L8Th7N8l7jADg3HRU44abmT: https://app.asana.com/api/1.0/projects/"+id_Project+"?opt_pretty";
String project_cmd = project_request;
project_cmd += " | grep 'OBJECT'";
Process ProjectshellOutput = systemShell.exec(workDir+"/"+project_cmd, null,workDir);
InputStreamReader Project_isr = new InputStreamReader(ProjectshellOutput.getInputStream());
BufferedReader Project_br = new BufferedReader (Project_isr);
JSONParser Project_jsonParser = new JSONParser();
JSONObject Project_jsonObject = (JSONObject) Project_jsonParser.parse(Project_br);
JSONObject projectdataObject= (JSONObject)Project_jsonObject.get("data");
Long project_id = (Long) projectdataObject.get("id");
System.out.println("project id is: " + project_id);
String project_created_at_dt = (String) projectdataObject.get("created_at");
System.out.println("The project created at is: " + project_created_at_dt);
String project_modified_at_dt = (String) projectdataObject.get("modified_at");
System.out.println("The project modified at is: " + project_modified_at_dt);
boolean project_public_status = (boolean) projectdataObject.get("public");
System.out.println("project public status: " + project_public_status);
String project_name = (String) projectdataObject.get("name");
System.out.println("project name is: " + project_name);
String project_notes = (String) projectdataObject.get("notes");
System.out.println("project Notes are: " + project_notes);
boolean project_archived_status = (boolean) projectdataObject.get("archived");
System.out.println("project archived status: " + project_archived_status);
JSONObject workspaceObj = (JSONObject) projectdataObject.get("workspace");
Long workspace_id = (Long) workspaceObj.get("id");
String workspace_name = (String) workspaceObj.get("name");
System.out.println("Workspace id " + workspace_id + " with workspace name " + workspace_name);
String color = (String) projectdataObject.get("color");
System.out.println("color : " + color);
JSONObject teamObj = (JSONObject) projectdataObject.get("team");
Long team_id = (Long) teamObj.get("id");
String team_name = (String) teamObj.get("name");
System.out.println("team id " + team_id + " with team name " + team_name);
It looks like you're using the JSON.simple library. According to this test, JSON.simple parsed a 190 MB file in about 140 seconds. That's in line with taking 9 seconds to parse a 15 MB file. Jackson is faster, but only just barely. So I doubt there's anything you can do to significantly improve performance.

How to get solution X prolog in java

I use jpl libraries to connect prolog and java. In prolog, I can execute query :
?- meaning_forms([apple,is,fruit],X).
output is : X = [is_a(x1, x2), objectx(x1, apple), objectx(x2, fruit)].
But in java, I can’t see output of this query. I tried some code in java :
Variable X = new Variable("X");
Query q4 = new Query("meaning_forms", new Term[]{new Atom("apple,is,fruit"),X});
while ( q4.hasMoreElements() ) {
java.util.Hashtable solution = (Hashtable) q4.nextElement();
System.out.println( "X = " + (Term) solution.get("X"));
}
There is no output in java . Any solution to this case?
Hashtable[] solutions = q4.allSolutions();
for (int i = 0 ; i < solutions.length; ++i) {
System.out.println("X = " + solutions[i].get(X));
}
See also http://www.swi-prolog.org/packages/jpl/java_api/getting_started.html

How to extract data from certain facebook pages for a specified date range(from and to)?

I am stuck up in this date range query. I need to extract data from particular facebook pages for a specified date range.I am able to do this individually, by using since and until fields. But how to use these two fields together.
Here is my code:
public static String getFacebookPostes(Facebook facebook, String searchPost)
throws FacebookException {
String searchResult = "Item : " + searchPost + "\n";
StringBuffer searchMessage = new StringBuffer();
ResponseList<Post> results = facebook.searchPosts(searchPost, new Reading().since("2014-04-02"));
String userId="";
for (Post post : results) {
System.out.println(post.getMessage());
searchMessage.append(post.getMessage() + "\n");
for (int j = 0; j < post.getComments().size(); j++) {
searchMessage.append(post.getComments().get(j).getFrom()
.getName()
+ ", ");
searchMessage.append(post.getComments().get(j).getMessage()
+ ", ");
searchMessage.append(post.getComments().get(j).getCreatedTime()
+ ", ");
searchMessage.append(post.getComments().get(j).getLikeCount()
+ "\n");
userId=post.getComments().get(j).getFrom().getId();
User user = facebook.getUser(userId);
//System.out.println("ROCK");
System.out.println(user);
}
}
Any guidance is appreciated. Thanks in advance.
PS : I am using facebook4j-core-2.0.2.jar and eclipse kepler.
According to http://facebook4j.org/en/code-examples.html you can use all date formats descirbed in http://www.php.net/manual/en/datetime.formats.date.php
From my understanding the code would then look like this:
ResponseList<Post> results = facebook.searchPosts(searchPost, new Reading().since("2014/04/02").until("2014/04/08"));

Categories