I am trying to access a single data from a JSON file with the url 'https://123.abc.com/student'. I need to display an individual data from this URL. Could someone help me with this?
My Json data looks like this
{"students":[{"_id":"5dc516e51bdd2c0014e32e65","name":"John","email":"john#gmail.com","mobile":"9988776655","__v":0},{"_id":"5dc517b51bdd2c0014e32e69","name":"Mark","email":"mark#gmail.com","mobile":"9834783927","__v":0},{"_id":"5dca9231cfba5c0014d3d765","name":"Glen","email":"glen#gmail.com","mobile":"9996474563","__v":0},{"_id":"5dcb898b9327cb00142169bb","name":"vijay","email":"vijay#gmail.com","mobile":"9884699633","__v":0},{"_id":"5dcb89c09327cb00142169bc","name":"tests","email":"test#gmail.com","mobile":"9887456123","__v":0},{"_id":"5dcb8caa9327cb00142169bd","name":"greens","email":"greens#gmail.com","mobile":"9876543210","__v":0},{"_id":"5dcb95d69327cb00142169be","name":"Akash","email":"a4akash6#gmail.com","mobile":"9846042118","__v":0},{"_id":"5dcb961c9327cb00142169bf","name":"Akash K M","email":"Akash.Madhu#cognizant.com","mobile":"9846042118","__v":0},{"_id":"5dcce611ad69f500149660d5","name":"efavewbvwevb","email":"indrajitrakurajravi#gmail.com","mobile":"9042758563","__v":0},{"_id":"5dcce874ad69f500149660d6","name":"efavewbvwevb","email":"indrajitrakurajravi#gmail.com","mobile":"9042758563","__v":0},{"_id":"5dcce968ad69f500149660d7","name":"indrajit","email":"indrajitrakurajravi#gmail.com","mobile":"9042758563","__v":0}]}
Accessing single and array of data
let data = {"students":[{"_id":"5dc516e51bdd2c0014e32e65","name":"John","email":"john#gmail.com","mobile":"9988776655","__v":0},{"_id":"5dc517b51bdd2c0014e32e69","name":"Mark","email":"mark#gmail.com","mobile":"9834783927","__v":0},{"_id":"5dca9231cfba5c0014d3d765","name":"Glen","email":"glen#gmail.com","mobile":"9996474563","__v":0},{"_id":"5dcb898b9327cb00142169bb","name":"vijay","email":"vijay#gmail.com","mobile":"9884699633","__v":0},{"_id":"5dcb89c09327cb00142169bc","name":"tests","email":"test#gmail.com","mobile":"9887456123","__v":0},{"_id":"5dcb8caa9327cb00142169bd","name":"greens","email":"greens#gmail.com","mobile":"9876543210","__v":0},{"_id":"5dcb95d69327cb00142169be","name":"Akash","email":"a4akash6#gmail.com","mobile":"9846042118","__v":0},{"_id":"5dcb961c9327cb00142169bf","name":"Akash K M","email":"Akash.Madhu#cognizant.com","mobile":"9846042118","__v":0},{"_id":"5dcce611ad69f500149660d5","name":"efavewbvwevb","email":"indrajitrakurajravi#gmail.com","mobile":"9042758563","__v":0},{"_id":"5dcce874ad69f500149660d6","name":"efavewbvwevb","email":"indrajitrakurajravi#gmail.com","mobile":"9042758563","__v":0},{"_id":"5dcce968ad69f500149660d7","name":"indrajit","email":"indrajitrakurajravi#gmail.com","mobile":"9042758563","__v":0}]}
//access name
console.log('name--', data.students[0].name)
//single object
console.log(data.students[0])
//access array of objects
data.students.map(d=> console.log(d))
You can use this
let data = {"students":[{"_id":"5dc516e51bdd2c0014e32e65","name":"John","email":"john#gmail.com","mobile":"9988776655","__v":0},{"_id":"5dc517b51bdd2c0014e32e69","name":"Mark","email":"mark#gmail.com","mobile":"9834783927","__v":0},{"_id":"5dca9231cfba5c0014d3d765","name":"Glen","email":"glen#gmail.com","mobile":"9996474563","__v":0},{"_id":"5dcb898b9327cb00142169bb","name":"vijay","email":"vijay#gmail.com","mobile":"9884699633","__v":0},{"_id":"5dcb89c09327cb00142169bc","name":"tests","email":"test#gmail.com","mobile":"9887456123","__v":0},{"_id":"5dcb8caa9327cb00142169bd","name":"greens","email":"greens#gmail.com","mobile":"9876543210","__v":0},{"_id":"5dcb95d69327cb00142169be","name":"Akash","email":"a4akash6#gmail.com","mobile":"9846042118","__v":0},{"_id":"5dcb961c9327cb00142169bf","name":"Akash K M","email":"Akash.Madhu#cognizant.com","mobile":"9846042118","__v":0},{"_id":"5dcce611ad69f500149660d5","name":"efavewbvwevb","email":"indrajitrakurajravi#gmail.com","mobile":"9042758563","__v":0},{"_id":"5dcce874ad69f500149660d6","name":"efavewbvwevb","email":"indrajitrakurajravi#gmail.com","mobile":"9042758563","__v":0},{"_id":"5dcce968ad69f500149660d7","name":"indrajit","email":"indrajitrakurajravi#gmail.com","mobile":"9042758563","__v":0}]}
let result = data.students.forEach(val=>{
console.log(val)
// let data individually as
let name = val.name
let email = val.email
})
I have an array of dictionaries that is being read in from a JSON file as seen below. I would like to store that value (jsonResult) into a class variable so that I can use it to populate a tableview. However, I don't quite understand how to store that value.
Here is how I am getting my array of dictionaries (jsonResult):
if let path = Bundle.main.path(forResource: filename, ofType: "json") {
do {
let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
let jsonResult = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as! [String:Any]
self.tableData = jsonResult // WHAT GOES HERE?
} catch {
// handle error
}
}
And this is my class variable that I want to store my array of dictionaries into:
var tableData = [Dictionary<String, String>]()
How can I correctly store jsonResult into tableData? I do not want to use a struct as the structure of the dictionaries can vary.
You state the JSON is an array of dictionary but you are casting the result of JSONSerialization.jsonObject to just a dictionary. Since you seem to be expected an array of dictionary with both string keys and values, cast the result accordingly. But do it safely. Never use ! when working with JSON.
if let jsonResult = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? [[String:String]] {
self.tableData = jsonResult
} else {
// Error - unexpected JSON result
}
This assumes you want the top level of the JSON result. If in fact jsonResult should be a dictionary and that top-level dictionary has a key to the actual array of dictionary you want then you need to fix the code accordingly.
I've got a server response returning
(
{
agreementId = "token.virtual.4321";
city = AMSTERDAM;
displayCommonName = "bunch-of-alphanumeric";
displaySoftwareVersion = "qb2/ene/2.7.14";
houseNumber = 22;
postalCode = zip;
street = "";
}
)
how do I get the value of agreementId? response['agreementId'] is not working. i've tried some example code with .first but I cannot get it working.
Some extra information, I do a http call to a server with alamofire. I try to parse the json to a constant response:
let response = JSON as! NSDictionary
However that returns a error message
Could not cast value of type '__NSSingleObjectArrayI' (0x1083600) to 'NSDictionary' (0x108386c).
So now parse the json to an array, which seems to be working. The code above is what
let response = JSON as! NSArry
print(response)
spits out.
Now I only need to retrieve the value from the key "agreementId" and I have no clue how to do that.
In swift you need to use Swift's native type Array/[] and Dictionary/[:] instead of NSArray and NSDictionary, if you specify the type like above means more specific then the compiler won't complain. Also use optional wrapping with if let or guard let to prevent crash.
if let array = JSON as? [[String:Any]] {//Swift type array of dictionary
if let dic = array.first {
let agreementId = dic["agreementId"] as? String ?? "N/A"//Set default value instead N/A
print(agreementId)
//access the other key-value same way
}
}
Note: If you having more than one object in your array then you need to simply loop through the array to access each dictionary of array.
if let array = JSON as? [[String:Any]] {//Swift type array of dictionary
for dic in array {
let agreementId = dic["agreementId"] as? String ?? "N/A"//Set default value instead N/A
print(agreementId)
//access the other key-value same way
}
}
How can I get the below output in jsp?
[[1417165200000,28477.92],[1417165320000,28484],[1417165440000,28474.86],[1417165560000,28478.88]]
Both the values comes from a variables in jsp code.
I have tried:
Map<String, String> list = new HashMap<String, String>();
list.put(last_traded_price, price_date);
It gives me this:
{28779.4400=2014-11-28 12:58:00.0, 28794.5000=2014-11-28 01:24:00.0}
Please suggest me what I can use in jsp to get the desired output.
This is what I did in php,
$array_item[] = array(strtotime($price_date)*1000, (float)$last_traded_price);
Update 1:
while(rs.next()){
last_traded_price = rs.getString(1);
price_date = rs.getString(2);
child.add(last_traded_price);//1
child.add(price_date);//2
}
If you want this [[1417165200000,28477.92],[1417165320000,28484]] particular format then i would advice you to use json-simple.jar
so if whenever you want to create data in above format you need to write code as below :
JSONArray root = new JSONArray();
while(rs.next()){
JSONArray child= new JSONArray();
last_traded_price = rs.getString(1);
price_date = rs.getString(2);
child.add(last_traded_price);//1
child.add(price_date);//2
root.add(child);
}
System.out.println(root);
so for 1417165200000,28477.92 this pair you will have root as [[1417165200000,28477.92]]
if you want to add another pair just repeat the steps 1,2 and 3
I have map with key and value and my goal is to get list of 'key'.
I am thinking to get it to the array or List.
Got to the point where I have key values in the SET but haven't figure out
how to convert to the array.
below is my code:
Map<String, String> mmm = new Map<String, String>();
mmm.put('one', 'oneee');
mmm.put('two', 'twooo');
mmm.put('three', 'threeee');
mmm.put('four', 'fourff');
//outputs values in the map
system.debug('=======values()==========>' + mmm.values());
//outputs key in the map
system.debug('=======keyset()===========>' + mmm.keyset());
//get keys in the type SET
SET<string> s = mmm.keyset();
//returns 4
system.debug('------------------------------------' + s.size());
s.arrayTo() //this method does not exist :(
Use List.addAll method?
http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_methods_system_list.htm?SearchType=Stem
If not - you could always manually loop through the set...
Could you use:
Set keys = mmm.keySet();
List keyList = new List(keys);
You should always used generics for type safety.
Map<String, String> mmm = new Map<String, String>();
mmm.put('one', 'oneee');
mmm.put('two', 'twooo');
mmm.put('three', 'threeee');
mmm.put('four', 'fourff');
List<String> lstKeys = new List<String>(mmm.keyset());
System.debug('Output : '+lstKeys);
As per link : https://salesforce.stackexchange.com/questions/5447/is-there-a-difference-between-an-array-and-a-list-in-apex .
This solution will work.
A quick and simple way to do this would also be:
new List<String>(mmm.keySet());
//or
new String[mmm.keySet()];
In some code I wrote recently I've got a Set<String> and am passing it into a method that takes a List<String> with methodName( new List<String>(setVariable) ); or methodName(new String[setVariable] );
Yes I know the post is 11+ years old... but it is also what comes up when searching so I put my answer here.