I have a JobDataMap Object
JobDataMap dataMap = context.getJobDetail().getJobDataMap();
String[] key = dataMap.getKeys();
key is foo as String,
key is data data have a json like this=> {"abc":"xyz","pqr":"123wer"}.
I want to get values from data object and set to String.
For eg: String abc = data.abc; Here I want to set value from data.
How do I get values from data object? Please help me....
solution is below:
String dataValue = dataMap.get("data").toString();
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(dataValue);
or we can also use objectMapper instead JSONParser.
String abc = (String) json.get("abc")
{
"0":"",
"54049":"ОП.мз-61а",
"100606":"КМ-41",
"100609":"МТ-41",
"100612":"ЕМ-41",
"100684":"ХК-51",
"100685":"ЕМ-51",
"100687":"КМ-51",
"100688":"МТ-51",
"100718":"ХК-51/1",
"100719":"ХК-51/2",
"100748":"ТС-61",
"100749":"ТС-61/1",
"100750":"ТС-61/2",
"100754":"ЕМ-61",
"100758":"ІМ-61/1МБ",
"100759":"ІМ-61/2ГТ",
"100760":"МБ-61",
"100767":"ТС-51",
"100770":"ТС-41",
"100777":"ТС.м-61",
"100778":"МТ.м-61",
"100779":"ЕМ.м-61",
"100780":"ТМ.м-61",
"100781":"ТМ.м-62",
"100782":"ГМ.м-61",
"100783":"ВІ.м-61",
"100786":"ХМ.м-61"
}
You'll need to iterate over the JSONObject keys and compare the value until a match.
final String knownValue = "TC-41";
final Iterator<String> iterable = jsonObject.keys();
while (iterable.hasNext()) {
String key = iterable.next();
if (jsonObject.getString(key).equals(knownValue)) {
// key has the value you are looking for
return;
}
}
Use JSON Classes for parsing
JSONObject mainObject = new JSONObject(Your_Sring_data);
String id = mainObject .getString("100770");
//here in this **id** string you can get value for **"TC-41"**
I have some function where I receive such JSON:
String sorting ->> {"state":"desc"}
String filter ->> {"clientID":"XML"}
All value within are always different. How can I get Key and Value for such JSON?
Lets say :
KEYsorting =state , Valuesorting = desc
KEYfilter =clientID, Valuefilter = XML
I have searched a lot - including this website - but couldn't dealt with it. I guess it is not very difficult I I wasn't able to do that. Please help
You need to get a list of all the keys, loop over them and add them to your map as shown in the example below:
String sorting = "{\"state\":\"desc\"}";
JSONObject filter = new JSONObject(sorting);
Map<String,String> map = new HashMap<String,String>();
Iterator iter = filter.keys();
while(iter.hasNext()){
String key = (String)iter.next();
String value = filter.getString(key);
map.put(key,value);
}
you can try above example.
I have a map of string objects and keys that I wish to put to a json file. I have read that the way to do it is by converting it to an array, and it only works with maps where both the object and key are strings. I can create a JSONObject from the map fine, but cannot put that to an array. Can someone tell me why this does not work?
private static final String JSON_USER_VOTES = "user_votes";
private Map<String, String> mCheckedPostsMap; //This is populated elsewhere
JSONObject obj=new JSONObject(mCheckedPostsMap);
JSONArray array=new JSONArray(obj.toString()); // <<< Error is on this line
json.put(JSON_USER_VOTES, array);
Here is the error:
org.json.JSONException: Value {"232":"true","294":"true"} of type org.json.JSONObject cannot be converted to JSONArray
If you want all of initial map entries enclosed in one JSON object, you can use:
JSONArray array = new JSONArray().put(obj);
This will produce something like
[{"key1:"value1","key2":"value2"}]
If you want each of initial map entries as different JSON object, you can use:
JSONObject obj = new JSONObject(map);
JSONArray array = new JSONArray();
for(Iterator iter = obj.keys(); iter.hasNext(); ){
String key = (String)iter.next();
JSONObject o = new JSONObject().put(key, map.get(key));
array.put(o);
}
This will produce something like
[{"key1:"value1"}, {"key2":"value2"}]
I am receiving a json string in the following format:
{"27":{"id":"27","uid":"4","title":"teamer.zapto.org","url":"www.google.jo","ip":"74.125.234.63","enabled":"1","services":{"Http 1":{"status":"1","ts":"1355058071"}},"fetch_interval":"60","ping_shift":"0"},
"30":{"id":"30","uid":"4","title":"google","url":"www.google.com","ip":"74.125.234.114","enabled":"1","services":{"Http 1":{"status":"1","ts":"1355058079"}},"fetch_interval":"60","ping_shift":"0"},
"31":{"id":"31","uid":"4","title":"facebook.com","url":"facebook.com","ip":"69.171.247.21","enabled":"1","services":{"Http 1":{"status":"1","ts":"1355058095"}},"fetch_interval":"60","ping_shift":"0"},
"32":{"id":"32","uid":"4","title":"ebir","url":"www.ebir.com","ip":"74.52.50.226","enabled":"1","services":{"Ftp":{"status":"1","ts":"1355058073"},"Http 1":{"status":"1","ts":"1355058073"}},"fetch_interval":"60","ping_shift":"0"},
"33":{"id":"33","uid":"4","title":"zapto","url":"teamer.zapto.org","ip":"200.35.150.6","enabled":"1","services":{"Http 1":{"status":"0","ts":"1355056146"}},"fetch_interval":"3600","ping_shift":"2"},
"35":{"id":"35","uid":"4","title":"vogella.com","url":"vogella.com","ip":"46.163.79.226","enabled":"1","services":{"Ftp":{"status":"1","ts":"1355058098"},"Http 1":{"status":"1","ts":"1355058098"}},"fetch_interval":"60","ping_shift":"0"},
"36":{"id":"36","uid":"4","title":"msn","url":"www.msn.com","ip":"131.253.13.140","enabled":"1","services":{"Http 1":{"status":"1","ts":"1355058103"}},"fetch_interval":"60","ping_shift":"0"},
"37":{"id":"37","uid":"4","title":"dubizzle.com","url":"www.dubizzle.com","ip":"94.236.93.152","enabled":"1","services":{"Http 1":{"status":"1","ts":"1355058068"}},"fetch_interval":"60","ping_shift":"0"},
"38":{"id":"38","uid":"4","title":"olx.jo","url":"olx.jo","ip":"204.74.99.100","enabled":"1","services":{"Http 1":{"status":"1","ts":"1355058108"}},"fetch_interval":"60","ping_shift":"0"},
"40":{"id":"40","uid":"4","title":"www.sukar.com","url":"www.sukar.com","ip":"72.52.8.195","enabled":"1","services":{"Ftp":{"status":"0","ts":"1355058092"},"Http 1":{"status":"1","ts":"1355058092"}},"fetch_interval":"60","ping_shift":"0"}}
as you can see the keys are numbers (27, 30, 31,...) and are not consecutive. How can I get the data from such json? I know that it must be some kind of loop depending on length but I couldn't figure it out on how to do so. Usually I'd use jObject.getString("id"), but since I don't know what the string would be what can I do?
If you have a JSONObject as the root, you should be able to do the following:
JSONObject root = new JSONObject(jsonString);
JSONArray names = root.names();
for(int i = 0; i < names.length(); i++) {
String tag = names.getString(i);
...
}
The tag will be the numeric tag you refer to.
JSONObject questionMark = new JSONObject(jsonString);
Iterator keys = questionMark.keys();
while(keys.hasNext()) {
// loop to get the dynamic key
String currentDynamicKey = (String)keys.next();
// get the value of the dynamic key
JSONObject currentDynamicValue = questionMark.getJSONObject(currentDynamicKey);