Convert array of dictionaries to JSON Format - ios

my api expects to receive JSON data in below form
{
"user_contacts" : [
{
"friend_list" : "+928885555512",
"user_id" : "1"
},
{
"friend_list" : "+925555648583",
"user_id" : "1"
},
{
"friend_list" : "+925555228243",
"user_id" : "1"
}
]
}
i have created this data through array of dictionaries and its working fine and it is stores in Data type Variable. As alamofire requires [String: Any] type of parameter so i have to convert that data into desired format so i tried this:
let my_dict = try JSONSerialization.jsonObject(with: jsonData!, options: []) as! [String: Any]
but when print this my_dict before call alamofire. it seems that it is not a correct json see below... with something like that: __NSArrayI 0x618000271dc0>
["user_contacts": <__NSArrayI 0x618000271dc0>(
{
"friend_list" = "+928885555512";
"user_id" = "1";
},
{
"friend_list" = "+925555228243";
"user_id" = "1";
},
{
"friend_list" = "+925554787672";
"user_id" = "1";
}
)
]
So how can i make this correct json form?
what i am doing wrong?
Updated:
let updated_User_No :[String:Any]=["friend_list": self.new_convert_telephone_no,"user_id": user_no];
user_outer_arrary.append(updated_User_No);
user_inner_array=["user_contacts": user_outer_arrary]
it Should be look like this:
{
"user_contacts" : [
{
"friend_list" : "+928885555512",
"user_id" : "1"
},
{
"friend_list" : "+925555648583",
"user_id" : "1"
}
]
}

Perform JSONSerialization as below:
let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? Dictionary<AnyHashable, Any>

Try this :
Create Structure
let users = ["" : ["user_contacts": [["friend_list":"user_id"],["friend_list3":"user_id3"]]]]
Create Json Object
let json = try JSONSerialization.jsonObject(with: data!, options:.prettyPrinted) as? [String : AnyObject]

Related

Parsing json string crashes

I'm trying to parse a json string:
if let jsonStr = asd.value(forKey: "orderData") as? String {
print(jsonStr)
let data = jsonStr.data(using: String.Encoding.utf8, allowLossyConversion: false)!
do {
let json = try JSONSerialization.jsonObject(with: data, options: []) as! [String: AnyObject] // CRASHES HERE
if let names = json["product_name"] as? [String] {
print(names)
}
} catch let error as NSError {
print("Failed to load: \(error.localizedDescription)")
}
}
But at the line let json = try JSONSeri... it crashes saying Could not cast value of type '__NSArrayI' to 'NSDictionary'.
Also tried changing this as! [String: AnyObject] to as! [[String: AnyObject]]. But it still doesn't work.
This is my json string structure:
[
{
"product_id" : "1",
"category_json" : {
"category_id" : "1",
"category_name" : "nvm"
},
"selling_price" : "200",
"product_name" : "nvm",
},
{
"product_id" : "2",
"category_json" : {
"category_id" : "2",
"category_name" : "cas"
},
"selling_price" : "800",
"product_name" : "cas",
}
]
You should not be force casting with ! unless you are 100% sure that it will succeed.
I would suggest you use the following:
let jsonArray = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [[String: Any]]
This will return you a list of products. If you want a list of the product names than you need to iterate over it and extract the product name of each item. You can do this like so:
let names = jsonArray.map({ $0["product_name"] as? String })
As already mentioned the object is an array, you have to use a for loop to get all items
...
let data = Data(jsonStr.utf8)
do {
if let json = try JSONSerialization.jsonObject(with: data) as? [[String: Any]] {
for item in json {
if let name = item["product_name"] as? String {
print(name)
}
}
}
} catch {
print("Failed to load: \(error.localizedDescription)")
}

Unable to convert json string to json object

let dictionaryToJsonObject: [String: Any]
let Dictionary: [String: Any] = ["FirstName": "John", "Email": "Jo#sm.com", "Password": "john#123", "LastName": "Smith", "Organization": ["Type": 5, "Name": "IT"]]
do {
let jsonData = try JSONSerialization.data(withJSONObject: Dictionary as Any, options: .prettyPrinted)
let jsonText = String(data: jsonData,
encoding: .ascii)
print("JSON string = \(jsonText!)")
if JSONSerialization.isValidJSONObject(jsonText!) {
print("Valid")
} else {
print("Not Valid")
}
}catch {
print(error.localizedDescription)
}
Output Will be : -
JSON string = {
"FirstName" : "John",
"Email" : "Jo#sm.com",
"Password" : "john#123",
"LastName" : "Smith",
"Organization" : {
"Type" : 5,
"Name" : "IT"
}
}
expected Result :
{
FirstName:"John",
Email:"Jo#sm.com",
Password:"john#123",
LastName:"Smith",
Organization:{
Type:5,
Name:"IT"
}
}
If you want to get it in json object form then your code should be like,
let dictionaryToJsonObject: [String: Any]
let Dictionary: [String: Any] = ["FirstName": "John", "Email": "Jo#sm.com", "Password": "john#123", "LastName": "Smith", "Organization": ["Type": 5, "Name": "IT"]]
do {
let jsonData = try JSONSerialization.data(withJSONObject: Dictionary as Any, options: .prettyPrinted)
let jsonObject = try JSONSerialization.jsonObject(with: jsonData, options: .init(rawValue: 0))
print("JSON string = \(jsonObject)")
if JSONSerialization.isValidJSONObject(jsonObject) {
print("Valid")
} else {
print("Not Valid")
}
}catch {
print(error.localizedDescription)
}
and your output will be,
JSON string = {
Email = "Jo#sm.com";
FirstName = John;
LastName = Smith;
Organization = {
Name = IT;
Type = 5;
};
Password = "john#123";
}
Valid
Ultimately it is your declared dictionary itself I think!

Unable to convert this JSON to a string

Please see below the json output
{
"queryLogs" : [
{
"status" : "false",
"query" : {
"contents" : {
"updated" : "",
"id" : 1488199579,
"created" : "",
"patient_count" : 60,
"isactive" : "1",
"status_id" : 0,
"starttime" : "",
"queue_status_id" : 0,
"date_consult" : ""
},
"conditions" : "{}"
},
"tableName" : "consultation",
"type" : "I",
"logId" : {
"id" : "261489537666",
"doctorId" : "100"
}
}
]
}
Need to convert above json into below format
{"queryLogs":[{ "logId":{"id":"76148951287","doctorId":"100"},
"tableName":"queue", "type":"I", "query":"{ \"contents\":{
\"patient_name\":\"queryLog Test\", \"status_id\":1,
\"queue_no\":\"6\", \"isactive\":1, \"id\":\"148956612\",
\"mobile\":\"9567969610\", \"updated\":\"2017-03-15 11:31:26
GMT+05:30\", \"created\":\"2017-03-15 11:31:26 GMT+05:30\",
\"consultation_id\":\"1495085636\"}, \"conditions\":{}
}","status":"false"}]}
First code is what i get when i convert the JSON, But how can i get the JSON like the second code.
i used below code to get the first output.
var f = ["queryLogs":[["status":"false","tableName":"consultation","type":"I","logId":ids,"query":logfile]]] as [String : Any]
let JSON = try? JSONSerialization.data(withJSONObject: f,
options:.prettyPrinted)
if let content = String(data: JSON!, encoding:
String.Encoding.utf8) {
print(content)
}
If you want response like that then you need to also make JSON string for your logfile dictionary also.
What you can do is make one extension of Dictionary, so that no need to write same code of JSONSerialization at every place.
extension Dictionary where Key: ExpressibleByStringLiteral {
var jsonString: String? {
guard let data = try? JSONSerialization.data(withJSONObject: self),
let string = String(data: data, encoding: .utf8) else {
return nil
}
return string
}
}
Now use this extension to get JSON string from your dictionaries.
let id‌​s = ["id" : "261489537666", "doctorId" : "100"]
let logfile = [
"contents" : [
"updated" : "",
"id" : 1488199579,
"created" : "",
"patient_count" : 60,
"isactive" : "1",
"status_id" : 0,
"starttime" : "",
"queue_status_id" : 0,
"date_consult" : ""
],
"conditions" : "{}"
] as [String : Any]
if let queryLogString = logfile.jsonString {
let f = ["queryLogs":[["status":"false","tableName":"consultation","‌​type":"I","logId": id‌​s,"query":queryLogString]]] as [String : Any]
if let content = f.jsonString {
print(content)
}
}
Output:
{"queryLogs":[{"status":"false","query":"{\"contents\":{\"updated\":\"\",\"id\":1488199579,\"created\":\"\",\"patient_count\":60,\"isactive\":\"1\",\"status_id\":0,\"starttime\":\"\",\"queue_status_id\":0,\"date_consult\":\"\"},\"conditions\":\"{}\"}","tableName":"consultation","‌​type":"I","logId":{"id":"261489537666","doctorId":"100"}}]}
once try like,
let data = try? JSONSerialization.data(withJSONObject: dic, options: JSONSerialization.WritingOptions(rawValue: 0))
if let content = String(data: data!, encoding:
String.Encoding.utf8) {
print(content)
}

Loop through JSON response from Alamofire

I am using xcode 7.2 and swift 2.1.1. using Alamofire for server communication. I have a tableview that display's dynamic data.Data includes username , useremail , profilePicture etc. I Tried to implement this code from stackoverflow. But I am getting a warning message says Cast from 'JSON' to unrelated type '[Dictionary]' always fails My json response is
{
"JsonRequestBehavior" : 1,
"MaxJsonLength" : null,
"ContentType" : null,
"Data" : {
"_id" : "5658444778a7531f4c79c23d",
"Photo" : "",
"AllowSharing" : "YES",
"MemberCount" : 5,
"Users" : [
{
"_id" : "5658443478a7531f4c79c23c",
"Photo" : "",
"MembershipDate" : "0001-01-01T00:00:00",
"MiddleName" : null,
"FirstName" : "Gohan",
"LastName" : null,
"Email" : "gohan#gmail.com"
},
{
"_id" : "566ea5f1dfead62350cf0fad",
"Photo" : "",
"MembershipDate" : "0001-01-01T00:00:00",
"MiddleName" : null,
"FirstName" : null,
"LastName" : null,
"Email" : "sachin#gmail.com"
}
],
"MembershipDate" : "2015-12-14T12:03:12.819Z",
"CreatedBy" : "5658443478a7531f4c79c23c"
},
"ContentEncoding" : null,
"RecursionLimit" : null
}
How can I loop through Users in JSON response ?
EDIT as per JohnyKutty's answer I tried the same code in my Project. The code for the same is
Alamofire.request(.GET,App.AppHomeURL() + "Load_Group", parameters: ["groupid":"\(groupId)"]).responseJSON
{
response in
print("\(response.data)")
switch response.result
{
case .Success(let _data):
let jsonData = JSON(_data)
print("Admin Response : \(jsonData)")
do
{
let json = try NSJSONSerialization.JSONObjectWithData(_data as! NSData, options: .AllowFragments) as! NSDictionary
if let DataObject = json["Data"] as? NSDictionary
{
if let users = DataObject["Users"] as? [NSDictionary]
{
for user in users
{
print("User id : \(user["_id"])")
}
}
}
}
catch let error as NSError
{
print(error.localizedDescription)
}
in this line let json = try NSJSONSerialization.JSONObjectWithData(_data as! NSData, options: .AllowFragments) as! NSDictionary
At first I used "_data" and then Xcode suggested a change and it changed from _data to _data as! NSData.
Your son structure is like following
JSON(Dictionary) -> Data(Dictionary) -> Users(Array of Dictionaries). So first you should pick the Users array from raw json then iterate through it.
Since alamofire is already serializing your response, No need to use JSONSerializer again, I am updating my answer.
UPDATE
Try this code inside the case
if let DataObject = _data["Data"] as? NSDictionary {
if let Users = DataObject["Users"] as? [NSDictionary] {
for user in Users {
print(user["_id"],user["MembershipDate"],user["FirstName"],user["Email"], separator: " ", terminator: "\n")
}
}
}
Full code:
Alamofire.request(.GET,App.AppHomeURL() + "Load_Group", parameters: ["groupid":"\(groupId)"]) .responseJSON { response in
switch response.result {
case .Success(let _data):
print(_data)
if let DataObject = _data["Data"] as? NSDictionary {
if let Users = DataObject["Users"] as? [NSDictionary] {
for user in Users {
print(user["_id"],user["MembershipDate"],user["FirstName"],user["Email"], separator: " ", terminator: "\n")
}
}
}
default:
break;
}
}

Alamofire Parameters - NSDictionary is not convertible to [String : AnyObject]

So I am trying to send a json object as parameters with Alamofire. I think I have followed example in their documentation but I get this error with the method call.
#IBAction func saveButton(sender: AnyObject) {
var url = "http://b857aaa4.ngrok.io/recruit"
let parameters = [
"_id" : firstNameTextField.text,
"alt_email" : "",
"forename" : firstNameTextField.text,
"phone" : recruteePhone.text,
"recruiter" : [
"comments" : "",//TODO comments
"date_met" : "",//TODO tdoays date
"email" : "",//TODO recruiter email
"event_name" : ""//TODO event name
],
"surname" : lastNameTextField.text
]
Alamofire.request(.PUT, url, parameters: parameters, encoding: .JSON)
.responseJSON { (req, res, json, error) in
if(error != nil) {
NSLog("Error: \(error)\n")
} else {
var json = JSON(json!);
println("success");
}
}
Declare parameters as
let parameters: [String: AnyObject] = [
and it compiles okay.

Resources