Getting first element of a JSON Array with Alamofire/SwiftyJSON
I have an Alamofire request, which gives me JSON as a result. Here is my JSON:
"user" : {
"field_first_name" : {
"und" : [
{
"value" : "Christopher",
"format" : null,
"safe_value" : "Christopher"
}
]
},
The "und" value is an array, which usually only has one value in it. I'd like to grab the first element of this array at all times, as occasionally this field will have multiple elements, but the first is always the correct result.
The code below works, until I get to the ".first". This returns a value of type (String, JSON) and I cannot figure out how to get this item "firstValue" back into usable format. Whenever I try to use
Alamofire.request(url, method: .post, parameters:parameters, headers: headers)
.responseJSON { response in
if let data = response.result.value {
let json = JSON(json: data)
let firstValue = json["user"]["field_first_name"]["und"].first
print(firstValue!)
}
}
firstValue then becomes of type (String, JSON) and I have no idea how to handle this. What I'd like to get is the value for key "value" out of "firstValue.
This is what "firstValue" prints out:
("0", {
"safe_value" : "Christopher",
"value" : "Christopher",
"format" : null
})
Is this approach salvageable? I could do a for in loop, get the first result, and tell it to ignore the rest, but that doesn't seem right. Thanks for any help.
json swift alamofire swifty-json
add a comment |
I have an Alamofire request, which gives me JSON as a result. Here is my JSON:
"user" : {
"field_first_name" : {
"und" : [
{
"value" : "Christopher",
"format" : null,
"safe_value" : "Christopher"
}
]
},
The "und" value is an array, which usually only has one value in it. I'd like to grab the first element of this array at all times, as occasionally this field will have multiple elements, but the first is always the correct result.
The code below works, until I get to the ".first". This returns a value of type (String, JSON) and I cannot figure out how to get this item "firstValue" back into usable format. Whenever I try to use
Alamofire.request(url, method: .post, parameters:parameters, headers: headers)
.responseJSON { response in
if let data = response.result.value {
let json = JSON(json: data)
let firstValue = json["user"]["field_first_name"]["und"].first
print(firstValue!)
}
}
firstValue then becomes of type (String, JSON) and I have no idea how to handle this. What I'd like to get is the value for key "value" out of "firstValue.
This is what "firstValue" prints out:
("0", {
"safe_value" : "Christopher",
"value" : "Christopher",
"format" : null
})
Is this approach salvageable? I could do a for in loop, get the first result, and tell it to ignore the rest, but that doesn't seem right. Thanks for any help.
json swift alamofire swifty-json
Try this answer, instead of using first use array and then first and let me know if it works
– carlos salzar
Jun 9 '17 at 16:19
I found a solution to this: firstValue is a tuple, of type (String, JSON), and I only need the second part of the tuple. So, if I do print(firstValue.1["value"]) it works
– cdeangelus
Jun 9 '17 at 18:01
add a comment |
I have an Alamofire request, which gives me JSON as a result. Here is my JSON:
"user" : {
"field_first_name" : {
"und" : [
{
"value" : "Christopher",
"format" : null,
"safe_value" : "Christopher"
}
]
},
The "und" value is an array, which usually only has one value in it. I'd like to grab the first element of this array at all times, as occasionally this field will have multiple elements, but the first is always the correct result.
The code below works, until I get to the ".first". This returns a value of type (String, JSON) and I cannot figure out how to get this item "firstValue" back into usable format. Whenever I try to use
Alamofire.request(url, method: .post, parameters:parameters, headers: headers)
.responseJSON { response in
if let data = response.result.value {
let json = JSON(json: data)
let firstValue = json["user"]["field_first_name"]["und"].first
print(firstValue!)
}
}
firstValue then becomes of type (String, JSON) and I have no idea how to handle this. What I'd like to get is the value for key "value" out of "firstValue.
This is what "firstValue" prints out:
("0", {
"safe_value" : "Christopher",
"value" : "Christopher",
"format" : null
})
Is this approach salvageable? I could do a for in loop, get the first result, and tell it to ignore the rest, but that doesn't seem right. Thanks for any help.
json swift alamofire swifty-json
I have an Alamofire request, which gives me JSON as a result. Here is my JSON:
"user" : {
"field_first_name" : {
"und" : [
{
"value" : "Christopher",
"format" : null,
"safe_value" : "Christopher"
}
]
},
The "und" value is an array, which usually only has one value in it. I'd like to grab the first element of this array at all times, as occasionally this field will have multiple elements, but the first is always the correct result.
The code below works, until I get to the ".first". This returns a value of type (String, JSON) and I cannot figure out how to get this item "firstValue" back into usable format. Whenever I try to use
Alamofire.request(url, method: .post, parameters:parameters, headers: headers)
.responseJSON { response in
if let data = response.result.value {
let json = JSON(json: data)
let firstValue = json["user"]["field_first_name"]["und"].first
print(firstValue!)
}
}
firstValue then becomes of type (String, JSON) and I have no idea how to handle this. What I'd like to get is the value for key "value" out of "firstValue.
This is what "firstValue" prints out:
("0", {
"safe_value" : "Christopher",
"value" : "Christopher",
"format" : null
})
Is this approach salvageable? I could do a for in loop, get the first result, and tell it to ignore the rest, but that doesn't seem right. Thanks for any help.
json swift alamofire swifty-json
json swift alamofire swifty-json
asked Jun 9 '17 at 14:29
cdeangeluscdeangelus
9114
9114
Try this answer, instead of using first use array and then first and let me know if it works
– carlos salzar
Jun 9 '17 at 16:19
I found a solution to this: firstValue is a tuple, of type (String, JSON), and I only need the second part of the tuple. So, if I do print(firstValue.1["value"]) it works
– cdeangelus
Jun 9 '17 at 18:01
add a comment |
Try this answer, instead of using first use array and then first and let me know if it works
– carlos salzar
Jun 9 '17 at 16:19
I found a solution to this: firstValue is a tuple, of type (String, JSON), and I only need the second part of the tuple. So, if I do print(firstValue.1["value"]) it works
– cdeangelus
Jun 9 '17 at 18:01
Try this answer, instead of using first use array and then first and let me know if it works
– carlos salzar
Jun 9 '17 at 16:19
Try this answer, instead of using first use array and then first and let me know if it works
– carlos salzar
Jun 9 '17 at 16:19
I found a solution to this: firstValue is a tuple, of type (String, JSON), and I only need the second part of the tuple. So, if I do print(firstValue.1["value"]) it works
– cdeangelus
Jun 9 '17 at 18:01
I found a solution to this: firstValue is a tuple, of type (String, JSON), and I only need the second part of the tuple. So, if I do print(firstValue.1["value"]) it works
– cdeangelus
Jun 9 '17 at 18:01
add a comment |
2 Answers
2
active
oldest
votes
I know this is over a year late, but this should work with your particular data. It's quite a long line of code though. I would break it up to make it more readable.
if let firstValue = json["user"]["field_first_name"] ["und"].arrayValue[0]["value"].string {
print(firstValue)
{
add a comment |
I believe you should be able to do something like this
if let firstValue = json["user"]["field_first_name"]["und"].first?.1 {
print(firstValue)
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44460417%2fgetting-first-element-of-a-json-array-with-alamofire-swiftyjson%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I know this is over a year late, but this should work with your particular data. It's quite a long line of code though. I would break it up to make it more readable.
if let firstValue = json["user"]["field_first_name"] ["und"].arrayValue[0]["value"].string {
print(firstValue)
{
add a comment |
I know this is over a year late, but this should work with your particular data. It's quite a long line of code though. I would break it up to make it more readable.
if let firstValue = json["user"]["field_first_name"] ["und"].arrayValue[0]["value"].string {
print(firstValue)
{
add a comment |
I know this is over a year late, but this should work with your particular data. It's quite a long line of code though. I would break it up to make it more readable.
if let firstValue = json["user"]["field_first_name"] ["und"].arrayValue[0]["value"].string {
print(firstValue)
{
I know this is over a year late, but this should work with your particular data. It's quite a long line of code though. I would break it up to make it more readable.
if let firstValue = json["user"]["field_first_name"] ["und"].arrayValue[0]["value"].string {
print(firstValue)
{
answered Oct 2 '18 at 16:26
skymookskymook
1,7962028
1,7962028
add a comment |
add a comment |
I believe you should be able to do something like this
if let firstValue = json["user"]["field_first_name"]["und"].first?.1 {
print(firstValue)
}
add a comment |
I believe you should be able to do something like this
if let firstValue = json["user"]["field_first_name"]["und"].first?.1 {
print(firstValue)
}
add a comment |
I believe you should be able to do something like this
if let firstValue = json["user"]["field_first_name"]["und"].first?.1 {
print(firstValue)
}
I believe you should be able to do something like this
if let firstValue = json["user"]["field_first_name"]["und"].first?.1 {
print(firstValue)
}
answered Jan 1 at 21:58


BrianBrian
858
858
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44460417%2fgetting-first-element-of-a-json-array-with-alamofire-swiftyjson%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Try this answer, instead of using first use array and then first and let me know if it works
– carlos salzar
Jun 9 '17 at 16:19
I found a solution to this: firstValue is a tuple, of type (String, JSON), and I only need the second part of the tuple. So, if I do print(firstValue.1["value"]) it works
– cdeangelus
Jun 9 '17 at 18:01