I am getting a response from an API
["response": {
record = {
title = "Order Add";
usercart = 5345;
};
}, "status": success, "message": تم إضافة السجل بنجاح]
I got the Value for status Key from the code
let statusCode = json["status"].string
Now i want the value for the key usercart in string value
I am using this code but getting any response.
let order_id = json["response"]["record"]["usercart"].string
Please help me to get this value.
As you are using SwiftyJSON and you are trying to get usercart value as a string but the usercart is Int.
So if you want this in string formate you should need to use .stringValue instead of .string else you can use .int or .intValue in form of int.
json["response"]["record"]["usercart"].stringValue
The json you are getting from server is wrong. Instead it should be -
["response": {
"record" : {
"title" : "Order Add",
"usercart" : 5345
};
}, "status": success, "message": تم إضافة السجل بنجاح]
There is nothing like = in json
Also if server response cannot change I would suggest reading this whole thing as string and then using search in string, though it is very bad approach.
if you are using SwiftyJSON then use this to get value
json["response"]["record"]["usercart"].stringValue
Related
I've been trying to retrieve the value of code for a while now. This is the json I'm getting back from the server:
{
"message": "The given data was invalid.",
"errors": {
"code": [
"The code has already been taken."
]
}
}
How do I get the value of "code" with swiftyJson?
I've already tried this but I don't think it's correct.
let list: Dictionary<String, JSON> = json["errors"].dictionaryValue
var retunValue = list.values.first
You can access the first element of the "code" array like this:
json["errors"]["code"].array?.first?.string
This will give you nil if code were an empty array, or the JSON isn't the expected structure.
Since "code" is an array, it might contain more than 1 string, so you might want a [String] instead:
json["errors"]["code"].array?.compactMap { $0.string } ?? []
This will give you an empty array if the JSON is not the expected structure.
I am trying to send following parameters in GET method call:
{
query = {
"$or" = (
{
name = yes;
},
{
status = Open;
}
);
};
}
But it seems it is not returning the proper URL:
baseURL/objects?query%5B%24or%5D%5B%5D%5Bname%5D=yes&query%5B%24or%5D%5B%5D%5Bstatus%5D=Open
I was expecting to "Or" my data, but it is doing "And".
I am using AFURLRequestSerialization class.
I have followed this SO, but it gives me all the object without applying any query.
AFNetworking GET parameters with JSON (NSDictionary) string contained in URL key parameter
It was working properly in POST call, but in GET it is not working as expected.
I have resolved this by converting dictionary against query key to a string and adding this string as a value of key query in the dictionary.
So my parameters will look like:
parameters: {
query = "{\"$or\":[{\"name\":\"yes\"},{\"status\":\"Open\"}]}";
}
Then I am passing this dictionary to AFNetworking.
I need to test an api endpoint where response of the endpoint will be like this
Response:
{
"items": [
{
"url": "http://www.localhost.com:8080/user?id=19909090"
}
]
}
I want to store the id value that is 19909090 to a variable. Can you please suggest some solution to achieve this ?
You can use JsonPath to read the value of url.
For example:
String url = from(json).get("$.items[0].url");
And then use java.net.URI to extract the query parameter value.
For example:
URI uri = URI.create(url);
String[] params = uri.getQuery().split("=");
// prints out 19909090
System.out.println(params[1]);
I can’t get the JSON data of message using swiftyjson.
When i print JSON value is there. But, when i print(json["result"]["message"]) it is null
{
"result": [{
"message": "success",
"age": "25"
}]
}
let json = JSON(data:jdata)
print(json)
print(json["result"]["message"])
json["result"] seems to be an array, you have to cast it to array like
let array = json["result"].arrayValue
let message = array[0]["message"]
You result is of array type. And you have to set index of object.
Try:
var array = json["result"].arrayValue
print(array[0]["message"])
You can also check this question
Hope it helps
Try:
let json = JSON(data: jdata)
let message = json["result"].array?.first?["message"]
print(message)
First of all, this is a noob question so anyone interested can answer me , because I'm just a beginner.
So, I need to parse JSON data from an api that my college seniors created for our annual technical festival. But they forgot to put a name to the array which contains all info.
1
They forgot to put the name and the role is similar to the "loans" at the beginning 2
I am using SwiftyJSON to parse the data and here is my code.
func getJSON(){
let url = NSURL(string: baseURL)
let request = NSURLRequest(URL:url!)
let session = NSURLSession(configuration:NSURLSessionConfiguration.defaultSessionConfiguration())
let task = session.dataTaskWithRequest(request) { (data, response, error) -> Void in
if error == nil{
let swiftyJSON = JSON(data:data!)
let loan1 = swiftyJSON["loans"][0]["status"].stringValue
print(loan1)
}else{
print("There is an error")
}
}
task.resume()
}
So i would like to know that what could "loans" be replaced by to get my JSON parsing up and running. I have already tried " " and "" to be sure.
Thanks !
Based on the JSON dictionary you posted, you aren't accessing any existing keys so loan1 can't possibly be anything other than nil. Based on the response in the first image, you get back an array of dictionaries.
[
{
"eid": 98,
"ename": "Lights. Camera. Tweet",
"sname": "IEEE GTBIT",
"ebanner": "http://s10.postimg.org/a11cezdft/12045587_1594466984176579_4873790458939193947_o.jpg",
"time": "1459189800854",
"category": "Literary & Management",
"desc": "Lights. Camera. Tweet! brings you a chance to win ..."
},
{
"eid": 45,
"ename": "Hack#GTBIT",
"sname": "Android Techies",
"ebanner": "http://i.imgur.com/ANR84uk.jpg",
"time": "1459222200397",
"category": "Technical",
"desc": "Hack#GTBIT is a Software Hackathon where particip..."
},
etc...
If none of this data is what you're looking for, you're not requesting properly or the data being returned isn't being properly formatted or including all the necessary information.
It's possible you could make a new request to check the status using the ID or some other value; you would access that through swiftyJSON[0]["eid"] – that would get you the value 98. But the key–value pair you're trying to access isn't correct based on the response. The "loans" key doesn't exist at the top level and the "status" key also does not exist.