I am new to swift and I am trying to get a link from a json file using Alamofire. I have been searching google for days now and have not found any useful solutions. I am not that good with json arrays.
I need the link from (_links.https://api.w.org/featuredmedia.href) this is my code:
guard let ide = self.json[0]["_links"]["https:\/\/api.w.org\/featuredmedia"]["href"].string else{
print("Request failed with error")
return
}
print(ide)
Alamofire.request(.GET, "http://tricitychurchofchrist.com/wp-json/wp/v2/ctc_sermon/5640").responseJSON { response in
guard let _ = response.result.value else{
print("Request failed with error")
return
}
}
And here is the json:
{
"id": 5854,
"date": "2016-01-17T20:22:28",
"date_gmt": "2016-01-17T20:22:28",
"guid": {
"rendered": "http:\/\/tricitychurchofchrist.com\/?post_type=ctc_sermon&p=5854"
},
"modified": "2016-01-18T19:16:10",
"modified_gmt": "2016-01-18T19:16:10",
"slug": "inviting-the-lightaudio-upload",
"type": "ctc_sermon",
"link": "http:\/\/tricitychurchofchrist.com\/sermons\/inviting-the-lightaudio-upload\/",
"title": {
"rendered": "Inviting The Light"
},
"content": {
"rendered": ""
},
"excerpt": {
"rendered": ""
},
"author": 1,
"featured_image": 2007,
"comment_status": "closed",
"ping_status": "closed",
"_links": {
"self": [{
"href": "http:\/\/tricitychurchofchrist.com\/wp-json\/wp\/v2\/ctc_sermon\/5854"
}],
"collection": [{
"href": "http:\/\/tricitychurchofchrist.com\/wp-json\/wp\/v2\/ctc_sermon"
}],
"about": [{
"href": "http:\/\/tricitychurchofchrist.com\/wp-json\/wp\/v2\/types\/ctc_sermon"
}],
"author": [{
"embeddable": true,
"href": "http:\/\/tricitychurchofchrist.com\/wp-json\/wp\/v2\/users\/1"
}],
"replies": [{
"embeddable": true,
"href": "http:\/\/tricitychurchofchrist.com\/wp-json\/wp\/v2\/comments?post=5854"
}],
"version-history": [{
"href": "http:\/\/tricitychurchofchrist.com\/wp-json\/wp\/v2\/ctc_sermon\/5854\/revisions"
}],
"https:\/\/api.w.org\/featuredmedia": [{
"embeddable": true,
"href": "http:\/\/tricitychurchofchrist.com\/wp-json\/wp\/v2\/media\/2007"
}],
"https:\/\/api.w.org\/attachment": [{
"href": "http:\/\/tricitychurchofchrist.com\/wp-json\/wp\/v2\/media?parent=5854"
}],
"https:\/\/api.w.org\/term": [{
"taxonomy": "ctc_sermon_topic",
"embeddable": true,
"href": "http:\/\/tricitychurchofchrist.com\/wp-json\/wp\/v2\/ctc_sermon\/5854\/ctc_sermon_topic"
}, {
"taxonomy": "ctc_sermon_book",
"embeddable": true,
"href": "http:\/\/tricitychurchofchrist.com\/wp-json\/wp\/v2\/ctc_sermon\/5854\/ctc_sermon_book"
}, {
"taxonomy": "ctc_sermon_series",
"embeddable": true,
"href": "http:\/\/tricitychurchofchrist.com\/wp-json\/wp\/v2\/ctc_sermon\/5854\/ctc_sermon_series"
}, {
"taxonomy": "ctc_sermon_speaker",
"embeddable": true,
"href": "http:\/\/tricitychurchofchrist.com\/wp-json\/wp\/v2\/ctc_sermon\/5854\/ctc_sermon_speaker"
}, {
"taxonomy": "ctc_sermon_tag",
"embeddable": true,
"href": "http:\/\/tricitychurchofchrist.com\/wp-json\/wp\/v2\/ctc_sermon\/5854\/ctc_sermon_tag"
}]
}}
I am using WP REST API fro the json from the wordpress site.
the data type you returnd is object type,you should change it to JSON object,you can use SwiftJSON open source on github,and code like this:
Alamofire.request(.GET, url, parameters: ["foo": "bar"])
.response { request, response, data, error in
let jsonval = JSON(data:data!)
let jsonDic:NSDictionary = jsonval.object as! NSDictionary
}
jsonDic is now a NSDictionary type,change NSDictionary to NSArray, and you can use it directly,hope help
Related
I'm really confused by API of web service
Some blocks of data has a format like that:
"ja": "犯罪\u8005の子孫た\u3061が暮\u3089\u3059スラム\u8857\u3002\u5883界線の\u5411\u3053\u3046の人\u3005か\u3089は「族民」\u3068\u3055\u3052\u3059まれ\u3001差\u5225\u3092受\u3051\u3066\u3044た\u3002孤\u5150\u3060\u3063た少年・ルドは\u3001育\u3066の親\u3067\u3042るレグト\u3068\u5171にスラム\u8857に住み\u3001常人離れ\u3057た身体能力\u3092武\u5668に生計\u3092立\u3066\u3066\u3044た\u3002\u3060が\u3042る日\u3001身に覚\u3048のな\u3044罪\u3092\u7740せ\u3089れ\u3001スラムの人\u3005\u3067\u3055\u3048\u6050れる「\u5948落」\u3078\u3068落\u3068\u3055れ\u3066\u3057ま\u3046\u2026\u2026\u3002"
I realized that Swift do not work with those format. But I really don't know in what step I should convert this
My parse func:
URLSession
.shared
.dataTask(with: request) { data, response, error in
if let data = data {
do {
if let sanitisedData = String(data: data, encoding: .utf8)!.map({ String($0) }).filter({ $0 != "\\" }).joined(separator: "").replacingOccurrences(of: "U(.*?)", with: "\\\\u$1", options: .regularExpression).data(using: .utf8) {
let decoder = JSONDecoder()
let decodedResponse = try decoder.decode(Welcome.self, from: sanitisedData)
DispatchQueue.main.async {
self.data = decodedResponse
}
}
} catch let jsonError as NSError {
print("JSON Decode Failed: \(jsonError.localizedDescription)")
}
return
}
}
.resume()
Output:
JSON Decode Failed: The data couldn’t be read because it isn’t in the correct format.
I tried to convert this data to string and that's the result:
犯罪u8005の子孫たu3061が暮u3089u3059スラムu8857u3002u5883界線のu5411u3053u3046の人u3005かu3089は「族民」u3068u3055u3052u3059まれu3001差u5225u3092受u3051u3066u3044たu3002孤u5150u3060u3063た少年・ルドはu3001育u3066の親u3067u3042るレグトu3068u5171にスラムu8857に住みu3001常人離れu3057た身体能力u3092武u5668に生計u3092立u3066u3066u3044たu3002u3060がu3042る日u3001身に覚u3048のなu3044罪u3092u7740せu3089れu3001スラムの人u3005u3067u3055u3048u6050れる「u5948落」u3078u3068落u3068u3055れu3066u3057まu3046u2026u2026u3002
Example of response:
{
"result": "ok",
"response": "collection",
"data": [
{
"id": "f2e906bb-8329-4f93-af70-b6344f18aa07",
"type": "manga",
"attributes": {
"title": {
"en": "This players think i\u2019m one of them"
},
"altTitles": [
{
"ru": "Э\u0442\u0438 \u0438\u0433\u0440ок\u0438 \u0434\u0443м\u0430ю\u0442, \u0447\u0442о я о\u0434\u0438н \u0438\u0437 н\u0438\u0445"
}
],
"description": {
"en": "I am the Dark Lord. The one who united the demons. One who wields limitless power and might. The one who has had enough of it all.\n\nThese people have attacked our lands again!\n\nFor a hundred years now.... vile players have attacked our hellish lands, mercilessly plundering and killing peaceful demons.\n\nToday I will become human, infiltrate the ranks of humanity and destroy their rotten system from within... while these players think I'm one of them.",
"ru": "Я - \u0422\u0435мны\u0439 по\u0432\u0435л\u0438\u0442\u0435ль. \u0422о\u0442, к\u0442о о\u0431ъ\u0435\u0434\u0438н\u0438л \u0434\u0435моно\u0432. \u0422о\u0442, к\u0442о о\u0431л\u0430\u0434\u0430\u0435\u0442 \u0431\u0435\u0441кон\u0435\u0447но\u0439 \u0432л\u0430\u0441\u0442ью \u0438 \u0441\u0438ло\u0439. \u0422о\u0442, ком\u0443 \u0432\u0441\u0435 э\u0442о н\u0430\u0434о\u0435ло.\n\nЭ\u0442\u0438 лю\u0434\u0438 \u0432 о\u0447\u0435\u0440\u0435\u0434но\u0439 \u0440\u0430\u0437 н\u0430п\u0430л\u0438 н\u0430 н\u0430\u0448\u0438 \u0437\u0435мл\u0438!\n\n\u0412о\u0442 \u0443\u0436\u0435 \u0441о\u0442ню л\u0435\u0442\u2026. М\u0435\u0440\u0437к\u0438\u0435 \u0438\u0433\u0440ок\u0438 \u0430\u0442\u0430к\u0443ю\u0442 н\u0430\u0448\u0438 \u0430\u0434\u0441к\u0438\u0435 \u0437\u0435мл\u0438, \u0431\u0435\u0441по\u0449\u0430\u0434но \u0433\u0440\u0430\u0431я \u0438 \u0443\u0431\u0438\u0432\u0430я м\u0438\u0440ны\u0445 \u0434\u0435моно\u0432.\n\n\u0412н\u0435\u0441\u0438\u0442\u0435 Э\u0422О!\n\n\u0421\u0435\u0433о\u0434ня я \u0441\u0442\u0430н\u0443 \u0447\u0435ло\u0432\u0435ком, п\u0440о\u0431\u0435\u0440\u0443\u0441ь \u0432 \u0440я\u0434ы \u0447\u0435ло\u0432\u0435\u0447\u0435\u0441\u0442\u0432\u0430 \u0438 \u0443н\u0438\u0447\u0442о\u0436\u0443 \u0438\u0445 п\u0440о\u0433н\u0438\u0432\u0448\u0443ю \u0441\u0438\u0441\u0442\u0435м\u0443 \u0438\u0437н\u0443\u0442\u0440\u0438... пок\u0430 э\u0442\u0438 \u0438\u0433\u0440ок\u0438 \u0434\u0443м\u0430ю\u0442, \u0447\u0442о я о\u0434\u0438н \u0438\u0437 н\u0438\u0445."
},
"isLocked": false,
"links": {
"raw": "https://remanga.org/manga/this-players-think-im-one-of-them?subpath=about"
},
"originalLanguage": "ru",
"lastVolume": "",
"lastChapter": "",
"publicationDemographic": "seinen",
"status": "ongoing",
"year": 2022,
"contentRating": "suggestive",
"tags": [
{
"id": "36fd93ea-e8b8-445e-b836-358f02b3d33d",
"type": "tag",
"attributes": {
"name": {
"en": "Monsters"
},
"description": [],
"group": "theme",
"version": 1
},
"relationships": []
},
{
"id": "391b0423-d847-456f-aff0-8b0cfc03066b",
"type": "tag",
"attributes": {
"name": {
"en": "Action"
},
"description": [],
"group": "genre",
"version": 1
},
"relationships": []
},
{
"id": "39730448-9a5f-48a2-85b0-a70db87b1233",
"type": "tag",
"attributes": {
"name": {
"en": "Demons"
},
"description": [],
"group": "theme",
"version": 1
},
"relationships": []
},
{
"id": "3e2b8dae-350e-4ab8-a8ce-016e844b9f0d",
"type": "tag",
"attributes": {
"name": {
"en": "Long Strip"
},
"description": [],
"group": "format",
"version": 1
},
"relationships": []
},
{
"id": "87cc87cd-a395-47af-b27a-93258283bbc6",
"type": "tag",
"attributes": {
"name": {
"en": "Adventure"
},
"description": [],
"group": "genre",
"version": 1
},
"relationships": []
},
{
"id": "a1f53773-c69a-4ce5-8cab-fffcd90b1565",
"type": "tag",
"attributes": {
"name": {
"en": "Magic"
},
"description": [],
"group": "theme",
"version": 1
},
"relationships": []
},
{
"id": "cdc58593-87dd-415e-bbc0-2ec27bf404cc",
"type": "tag",
"attributes": {
"name": {
"en": "Fantasy"
},
"description": [],
"group": "genre",
"version": 1
},
"relationships": []
},
{
"id": "e197df38-d0e7-43b5-9b09-2842d0c326dd",
"type": "tag",
"attributes": {
"name": {
"en": "Web Comic"
},
"description": [],
"group": "format",
"version": 1
},
"relationships": []
},
{
"id": "f5ba408b-0e7a-484d-8d49-4e9125ac96de",
"type": "tag",
"attributes": {
"name": {
"en": "Full Color"
},
"description": [],
"group": "format",
"version": 1
},
"relationships": []
}
],
"state": "published",
"chapterNumbersResetOnNewVolume": false,
"createdAt": "2022-01-10T17:15:53+00:00",
"updatedAt": "2022-01-10T17:26:26+00:00",
"version": 4,
"availableTranslatedLanguages": [
"ru"
]
},
"relationships": [
{
"id": "5537d8ed-16ed-4f6f-af75-ad7d7edb2ddc",
"type": "author"
},
{
"id": "36ca9e78-35a9-474d-b4e6-0e4065f0af87",
"type": "artist"
},
{
"id": "93e21cc1-1f61-46ce-8362-90d9d6888f63",
"type": "artist"
},
{
"id": "0e703bf4-1996-432c-8620-3c08f2edb37b",
"type": "cover_art"
}
]
}
],
"limit": 1,
"offset": 0,
"total": 57
}
If your server response is really without the unicode escaped, that is,
"犯罪\u8005の子孫た\u3061 ...", then try escaping the data as shown in this example code,
to get: "犯罪\\u8005の子孫た\\u3061 ..." (note the \\) and decode it like this:
URLSession
.shared
.dataTask(with: request) { data, response, error in
if let data = data {
do {
if let sanitisedData = String(data: data, encoding: .utf8)!
.replacingOccurrences(of: "\\", with: "\\\\") // <-- here
.data(using: .utf8) {
let decoder = JSONDecoder()
let decodedResponse = try decoder.decode(Welcome.self, from: sanitisedData)
DispatchQueue.main.async {
self.data = decodedResponse
}
}
} catch let jsonError as NSError {
print("JSON Decode Failed: \(jsonError.localizedDescription)")
}
return
}
}.resume()
EDIT-1:
you may also need to do this: .replacingOccurrences(of: "\n", with: "\\n")
You don't need to process the data returned at all if the result is simply a text (string to say welcome someone). My solution below is just loading a JSON file returned (as you mentioned above) and then decode the data to Welcome object below
struct Welcome: Codable {
let ja: String
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
ja = try values.decodeIfPresent(String.self, forKey: .ja) ?? ""
}
}
func readJsonFile(filename: String) -> String {
guard let fileUrl = Bundle.main.url(forResource: filename, withExtension: "json") else { fatalError() }
guard let jsonData = try? String(contentsOf: fileUrl) else {
return ""
}
return jsonData
}
func parseJsonFile() {
let jsonStr = readJsonFile(filename: "data")
let jsonData: Data = Data(jsonStr.utf8)
let decoder = JSONDecoder()
do {
let welcome = try decoder.decode(Welcome.self, from: jsonData)
print("\(welcome.ja)")
} catch {
print(error.localizedDescription)
}
}
this is the result when I print the welcome.ja's value:
犯罪者の子孫たちが暮らすスラム街。境界線の向こうの人々からは「族民」とさげすまれ、差別を受けていた。孤児だった少年・ルドは、育ての親であるレグトと共にスラム街に住み、常人離れした身体能力を武器に生計を立てていた。だがある日、身に覚えのない罪を着せられ、スラムの人々でさえ恐れる「奈落」へと落とされてしまう……。
I am new to Python/Scala. Trying to read/parse a JSON file from dataLake using Python and Scala but unsuccessful. Is there something wrong with the .JSON schema. I have attached the JSON schema and the data frame screenshots. Any help would be highly appreciated.
{
"result": [
{
"instance": {
"link": "https://service-now.com/api/now/table/assessment_instance/3d2a2c44d720058ba859e03d2",
"value": "3d2a2c44d720058ba859e03d2"
},
"string_value": "Very Good",
"assign_to_order": "",
"instance_question": {
"link": "https://service-now.com/api/now/table/assessment_instance_question/022a2c44d720058ba859e03d7",
"value": "022a2c44d720058ba859e03d7"
},
"template_definition": "",
"metric_definition": {
"link": "https://service-now.com/api/now/table/metric_definition/7758eb387233200dbba82cb0bfb",
"value": "7758eb387233200dbba82cb0bfb"
},
"sys_updated_on": "207-07-0 02:3:3",
"scaled_value": "4",
"metric": {
"link": "https://service-now.com/api/now/table/metric/542a2c44d720058ba859e03a4",
"value": "542a2c44d720058ba859e03a4"
},
"sys_created_on": "203-2-04 2:34:08",
"sys_domain": {
"link": "https://service-now.com/api/now/table/sys_user_group/global",
"value": "global"
},
"weighted_value": "0.8",
"source_id": {
"link": "https://service-now.com/api/now/table/type/502a2c44d720",
"value": "502a2c44d720058ba859e03a3"
},
"normalized_value": ".33",
"source_table": "metric_type",
"user": "",
"sys_created_by": "admin"
}
]
}
val asmt_metric_result = spark.read.option("multiline", "true")
.json(s"c:/servicenow/asmt_metric_result/2021/09/28/08/32/asmt_metric_result_000000.json")
asmt_metric_result.show(false)
I just want to be able to go through the individual gs files as blobs to summarize some information about the entire project. I think I CAN do this with drive api but I need to know the individual files first.
Solution:
Yes, you can do this with Drive API:
First you need to use DriveApp.getFilesByType("application/vnd.google-apps.script") to get the list of Apps Script projects in your Drive.
Sample List:
{
"kind": "drive#fileList",
"etag": "\"kjsas92/f3zGUXczKMxEB_9ZTMRFOF3d1ZU\"",
"selfLink": "https://www.googleapis.com/drive/v2/files?q=mimeType%3D'application/vnd.google-apps.script'+and+'me'+in+owners",
"items": [
{
"kind": "drive#file",
"id": "1vi0uwcMdHsRv1YFtgq7XdiTGSdqgjIYpdQNC0A_Udn79LOhH0vYL132D",
"etag": "\"kjsas92/MTM3MDk3ODY5ODQyNg\"",
"selfLink": "https://www.googleapis.com/drive/v2/files/1vi0uwcMdHsRv1YFtgq7XdiTGSdqgjIYpdQNC0A_Udn79LOhH0vYL132D",
"alternateLink": "https://script.google.com/a/google.com/d/1vi0uwcMdHsRv1YFtgq7XdiTGSdqgjIYpdQNC0A_Udn79LOhH0vYL132D/edit?usp=drivesdk",
"iconLink": "https://ssl.gstatic.com/docs/doclist/images/icon_11_script_list.png",
"title": "Mail merge",
"mimeType": "application/vnd.google-apps.script",
"description": "",
"labels": {
"starred": false,
"hidden": false,
"trashed": true,
"restricted": false,
"viewed": true
},
"createdDate": "2013-06-11T19:24:45.188Z",
"modifiedDate": "2013-06-11T19:24:58.426Z",
"modifiedByMeDate": "2013-06-11T19:24:58.426Z",
"lastViewedByMeDate": "2013-06-11T19:24:58.426Z",
"parents": [
{
"kind": "drive#parentReference",
"id": "0APdyIOzo7bWDUk9PVA",
"selfLink": "https://www.googleapis.com/drive/v2/files/1vi0uwcMdHsRv1YFtgq7XdiTGSdqgjIYpdQNC0A_Udn79LOhH0vYL132D/parents/0APdyIOzo7bWDUk9PVA",
"parentLink": "https://www.googleapis.com/drive/v2/files/0APdyIOzo7bWDUk9PVA",
"isRoot": true
}
],
"exportLinks": {
"application/json": "https://script.google.com/feeds/download/export?id=1234567890abcefghijklmnopqrstuvwxyz&format=json"
},
"userPermission": {
"kind": "drive#permission",
"etag": "\"kjsas92/259X2r5DVstv1CcIQTjt_RQPSW8\"",
"id": "me",
"selfLink": "https://www.googleapis.com/drive/v2/files/1vi0uwcMdHsRv1YFtgq7XdiTGSdqgjIYpdQNC0A_Udn79LOhH0vYL132D/permissions/me",
"role": "owner",
"type": "user"
},
"quotaBytesUsed": "0",
"ownerNames": [
"John Doe"
],
"owners": [
{
"kind": "drive#user",
"displayName": "John Doe",
"picture": {
"url": "https://lh4.googleusercontent.com/-yd1rIb6Pe2Y/AAAAAAAAAAI/AAAAAAAAAGs/PP5vTuZonik/s64/photo.jpg"
},
"isAuthenticatedUser": true,
"permissionId": "1234566789"
}
],
"lastModifyingUserName": "John Doe",
"lastModifyingUser": {
"kind": "drive#user",
"displayName": "John Doe",
"picture": {
"url": "https://lh4.googleusercontent.com/-yd1rIb6Pe2Y/AAAAAAAAAAI/AAAAAAAAAGs/PP5vTuZonik/s64/photo.jpg"
},
"isAuthenticatedUser": true,
"permissionId": "1234566789"
},
"editable": true,
"writersCanShare": true,
"shared": false,
"explicitlyTrashed": true,
"appDataContents": false
}
]
}
Then each individual project has exportLinks property that has a link to the scripts inside. You need to fetch that URL to get a JSON response that has the individual files and script ID's.
Sample JSON:
{
"files": [
{
"id":"9basdfbd-749a-4as9b-b9d1-d64basdf803",
"name":"Code",
"type":"server_js",
"source":"function doGet() {\n return HtmlService.createHtmlOutputFromFile(\u0027index\u0027);\n}\n"
},
{
"id":"3asf7c0d-1afb-4a9-8431-5asdfc79e7ae",
"name":"index",
"type":"html",
"source":"\u003chtml\u003e\n \u003cbody\u003e\n Hello, world!\n \u003c/body\u003e\n\u003c/html\u003e"
}
]
}
References:
Import and Export Projects
Get Files by Type
Files Resource
I am trying to map an array of objects using Object Mapper
I have this code so far, and my mapping is not successful
do {
if let data = data, let sectorData = Mapper<SubSectorsModel>().mapArrayOfArrays(JSONObject: try JSONSerialization.data(withJSONObject: data, options: [])) {
completionHandler(sectorData,(response as! HTTPURLResponse), error)
print("SectionData Received Successfully")
}
} catch {
completionHandler(nil,(response as! HTTPURLResponse), error)
print("Error parsing json get sector data: ", error.localizedDescription)
}
My Json data is as follows:
[
{
"SECTOR_NAME": "MANUFACTURERS",
"ID": "8",
"SECTOR": [
{
"ID": "144",
"NAME": "Biomass Processing"
},
{
"ID": "8",
"NAME": "Servicing engines and motors"
},
{
"ID": "23",
"NAME": "Furniture & fittings"
},
{
"ID": "31",
"NAME": "Fabrics & textiles"
},
{
"ID": "20",
"NAME": "Hand and machine tools"
},
{
"ID": "28",
"NAME": "Safety and security products"
},
{
"ID": "147",
"NAME": "Jewellery"
},
{
"ID": "156",
"NAME": "Beverages"
},
{
"ID": "165",
"NAME": "Stationery"
},
{
"ID": "9",
"NAME": "Industrial equipment"
},
{
"ID": "25",
"NAME": "Cleaning equipment"
},
{
"ID": "33",
"NAME": "Household consumer products"
},
{
"ID": "162",
"NAME": "Paper Products"
},
{
"ID": "170",
"NAME": "Memoribilia"
},
{
"ID": "143",
"NAME": "Food Products"
},
{
"ID": "22",
"NAME": "Automotive aviation marine and rail products"
},
{
"ID": "30",
"NAME": "Household appliances"
},
{
"ID": "151",
"NAME": "Iron Sheet"
},
{
"ID": "167",
"NAME": "Cosmetics"
},
{
"ID": "11",
"NAME": "Fuel Lubricants & Detergents"
},
{
"ID": "19",
"NAME": "Electrical appliances and equipment"
},
{
"ID": "27",
"NAME": "Packaging products"
},
{
"ID": "7",
"NAME": "Engines & parts"
},
{
"ID": "24",
"NAME": "Glass products"
},
{
"ID": "32",
"NAME": "Clothing & footwear"
},
{
"ID": "152",
"NAME": "Building Material"
},
{
"ID": "142",
"NAME": "Food Processing and Packaging"
},
{
"ID": "21",
"NAME": "Plastic products"
},
{
"ID": "29",
"NAME": "Pool & garden products"
},
{
"ID": "157",
"NAME": "Steel Products"
},
{
"ID": "138",
"NAME": "Optical Prescription Lenses"
},
{
"ID": "10",
"NAME": "Servicing & refurbishing"
},
{
"ID": "18",
"NAME": "Chemical"
},
{
"ID": "26",
"NAME": "Board paper and"
}
]
},
.
.
.
]
If you just want to send completionHandler anyway, use code below:
struct MyJsonStruct : Decodable {
struct SectorStruct : Decodable {
var ID : String
var NAME : String
}
var SECTOR_NAME : String
var ID : String
var SECTOR : [SectorStruct]
}
func handle(_ data : Data ) {
do {
let sectorData = try JSONDecoder().decode(MyJsonStruct.self, from: data) as MyJsonStruct
let yourArray = sectorData.SECTOR // if you need an array result
completionHandler(sectorData,(response as! HTTPURLResponse), error)
print("SectionData Received Successfully")
} catch {
completionHandler(nil,(response as! HTTPURLResponse), error)
print("Error parsing json get sector data: ", error.localizedDescription)
}
}
I have the following multiple json objects to post data using Alamofire. In which format I need to pass the parameters in swift. I am new to swift . Help me out.
{
"refno": "",
"ddesc": "",
"free": "0",
"fgift": "",
"sgift": "",
"sandage": {
"bank": "",
"bag": ""
},
"inst": "",
"items": [{
"itemid": "606",
"qty": "1",
"sub": [{
"item": "1586",
"qty": "1",
"type": "addon",
"ext": ""
}, {
"item": "1588",
"qty": "1",
"type": "addon",
"ext": ""
}, {
"item": "1589",
"qty": "1",
"type": "addon",
"ext": ""
}, {
"item": "1590",
"qty": "1",
"type": "addon",
"ext": ""
}]
}, {
"itemid": "639",
"qty": "1",
"sub": [{
"item": "1618",
"qty": "1",
"type": "addon",
"ext": ""
}, {
"item": "1612",
"qty": "1",
"type": "addon",
"ext": ""
}, {
"item": "1611",
"qty": "1",
"type": "addon",
"ext": ""
}, {
"item": "1610",
"qty": "1",
"type": "addon",
"ext": ""
}]
}],
"discount": "0",
"coupon": [],
"delivery": "",
"user": {
"id": "13",
"fname": "Demo",
"lname": "Order",
"phone": "9876543210",
"dno": "",
"add1": "",
"add2": "",
"postcode": "",
"username": "demo#theepos.com",
"status": "1"
},
"otype": "1",
"ptype": "0",
"app_id": "A1A2A3A4",
"app_key": "K1K2K3K4",
"request": "placeorder"
}
I am using Alamofire to post the data..
var url: NSURL = NSURL(string: "http://\(platform).eposapi.co.uk")!
let params = [
"refno": "",
"ddesc": "",
"free": "0",
"fgift": "",
"sgift": "",
.....
]
Alamofire.request(.POST, url, parameters: params, encoding: .JSON)
.responseJSON{ response in
if let result: AnyObject = response.result.value
{
let post: JSON = JSON(result)
}
}
Really thanks guys . I only followed the above commented instructions and got the answer. Help really appreciated
var url: NSURL = NSURL(string: "http://\(platform).eposapi.co.uk")!
let sandage = ["bank": "","bag": ""]
let sub_array = [ "item": "1586","qty": "1","type": "addon","ext": ""]
let items_array = ["itemid": "606","qty": "1","sub": sub_array ]
let user_Detail = ["id": "13","fname": "Demo","lname": "Order",
"phone": "9876543210","dno": "","add1": "",
"add2": "","postcode": "","username": "demo#theepos.com",
"status": "1"]
let params = [
"refno": "",
"ddesc": "",
"free": "0",
"fgift": "",
"sgift": "",
"sandage": sandage,
"inst": "",
"items":items_array,
"discount": "0",
"coupon": [],
"delivery": "",
"user": user_Detail,
"otype": "1",
"ptype": "0",
"app_id": "A1A2A3A4",
"app_key": "K1K2K3K4",
"request": "placeorder"
]
Alamofire.request(.POST, url, parameters: params, encoding: .JSON)
.responseJSON{ response in
if let result: AnyObject = response.result.value
{
let post: JSON = JSON(result)
let status = post["status"].stringValue
let order_id = post["order_id"].stringValue
print(status)
print(order_id)