Reputation: 45
How do you get a value from a json object, if JsonArray
type is 'Personal'? I would like a a name value pair like this:
String s = "February" ;
int i = 3;
I would really appreciate some advice on how to proceed further.
Thanks in advance, Sankar
String monthArray[] = { "January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November",
"December" };
System.out.println("Size of month array:-->> " + monthArray.length);
JSONObject jbj = new JSONObject();
JSONObject jbj1 = new JSONObject();
JSONObject jbj2 = new JSONObject();
JSONObject general = new JSONObject();
JSONArray jarray = new JSONArray();
JSONArray jarray1 = new JSONArray();
JSONArray jarray2 = new JSONArray();
JSONObject finalGen = new JSONObject();
jbj.put("February", 3);
jbj.put("March", 15);
jbj.put("April", 6);
jbj.put("July", 10);
jbj.put("December", 5);
jbj1.put("February", 3);
jbj1.put("March", 14);
jbj1.put("April", 23);
jbj1.put("May", 6);
jbj2.put("February", 33);
jbj2.put("March", 12);
jbj2.put("May", 4);
jbj2.put("June", 7);
jarray.add(jbj);
jarray1.add(jbj1);
jarray2.add(jbj2);
general.put("Personal", jarray);
general.put("Common", jarray1);
general.put("Management", jarray2);
finalGen.put("FinalResultSet", general);
System.out.println("\n Personal Value :-->> " + finalGen.toString(3));
JSONObject finalson = JSONObject.fromObject(finalGen
.get("FinalResultSet"));
JSONArray jarra = finalson.getJSONArray("Personal");
for (int i = 0; i < jarra.size(); i++) {
JSONObject jb = jarra.getJSONObject(i);
for (int p = 0; p < monthArray.length; p++) {
String s = monthArray[p];
System.out.println("\n VALUE OF STR " + s);
String t = jb.getString(s);
System.out.println("\n VALUE OF STR Json Str " + t);
}
}
System.out.println("Personal String " + jarra);
}
Size of month array:-->> 12
Personal Value :-->> {"FinalResultSet": {
"Personal": [ {
"February": 3,
"March": 15,
"April": 6,
"July": 10,
"December": 5
}],
"Common": [ {
"February": 3,
"March": 14,
"April": 23,
"May": 6
}],
"Management": [ {
"February": 33,
"March": 12,
"May": 4,
"June": 7
}]
}}
VALUE OF STR January
Exception in thread "main" net.sf.json.JSONException: JSONObject["January"] not found.
at net.sf.json.JSONObject.getString(JSONObject.java:2177)
at JFreechartDemo.XMLData.main(XMLData.java:68)
Upvotes: 1
Views: 1416
Reputation: 1
private void doLogin(String userName, String password) {
//http://obbg.in/api/login.php?action=login&username=tejal+patel+&password=ZGhhcmE5ODk4MjQ4Mzg3%0A
byte[] data = new byte[0];
try {
data = password.getBytes("UTF-8");
password = Base64.encodeToString(data, Base64.DEFAULT);
Ion.with(this)
.load("http://obbg.in/api/login.php")
.setBodyParameter("action", "login")
.setBodyParameter("username", URLEncoder.encode(userName, "utf-8"))
.setBodyParameter("password", password)
.asString()
.setCallback(this);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
@Override
public void onCompleted(Exception e, String result) {
try {
JSONObject jsonObject = new JSONObject(result);
JSONArray username = jsonObject.optJSONArray("Result");
etLoginPassword.setText("");
} catch (JSONException e1) {
e1.printStackTrace();
}
}
Upvotes: 0
Reputation: 2158
You are trying to get value of field with key "January" inside your for
loop, but there is no such field.
You should first put key-value pair like that into your JSON object.
Have a look at JSONObject get
method documentation. There is something like that there:
Throws:
JSONException - if the key is not found.
Because get method throws exception if there is no key it's not a really good idea to iterate through the whole array with months and trying to guess if the specific month is inside JSON as a key. You can do it if you want, but you should use has
method first to verify if month really exists.
On the other hand you can get all names of the keys from JSONObject and then call get
method only on those names.
Upvotes: 1