Using NSJSONSerialization.JSONObjectWithData(..) we can create a JSON object. But is it possible to identify the object type ie. is it a JSON object or not.
I was just trying to check using Swift and XCTestFramework. I tried different ways but no solution still?
Note: After creation of JSON object, I can get the values and can also check the values. XCTest Framework is working fine to test those type of things. But, I stuck to identify the object type.
Anybody has any idea how to identify the JSON object programmatically using Swift and XCTest framework
Update: For example, for a website testing we can do the following:
if let HTTPResponse = response as? NSHTTPURLResponse,
responseURL = HTTPResponse.URL,
MIMEType = HTTPResponse.MIMEType
{
XCTAssertEqual(responseURL.absoluteString, URL.absoluteString, "HTTP response URL should be equal to original URL")
XCTAssertEqual(HTTPResponse.statusCode, 200, "HTTP response status code should be 200")
XCTAssertEqual(MIMEType, "text/html", "HTTP response content type should be text/html")
} else {
XCTFail("Response was not NSHTTPURLResponse")
}
Is something possible like above for JSON?
If you get a valid string within your NSData parameter, but that String is not a valid JSON object, then the parser will throw an error. As from the documentation:
If an error occurs, upon return contains an NSError object that describes the problem.
So check if the object returned is actually an NSError. If you don't get an error, then I would safely assume that the object is indeed a valid JSON object.
Related
The response has been returned as Iterable object. I need to return any response as json response.
Example, the below returns a different return type
ClientEntitySetIterator<ClientEntitySet, ClientEntity> iterator =
readEntities(edm, serviceUrl, "employee");
Instead of iterator need to return the content as JSON directly or retrieve the response as JSON without having any mapper or custom code.
Please advise.
I'm testing an app not yet published. I have a client side register/log-in, using Alamofire to post and then retrieve and parse JSON. Unless Alamofire has "blackboxed" some type of hash, I am not aware of having coded any kind of hash anywhere, yet.
When I went to look into why the test passwords (passed via SSL, HTTPS, and without any effort to encrypt, yet) were showing up on the server side looking like the result of a hash, I compared that result to a deliberate server side Sha256 hash (done on the raw, plain text password matching the original that got passed from the app). I am seeing this:
"ccc" ----> Sha256 hash = 64daa44ad493ff28a96effab6e77f1732a3d97d83241581b37dbd70a7a4900fe
"ccc" ----> "simple iOS post" (via Alamofire) = 9df62e693988eb4e1e1444ece0578579
As you can see, the values are very different, and this means unless I know what happened on the way over, I cannot authenticate anyone on the server side, nor can I use any server side password reset functions, because I have no idea what kind of hash was used.
Can anyone help me know what happened to hash the password?
Here's the simple Alamofire-based code doing the post (Xcode 9, Swift 4):
//creating parameters for the post request
let parameters: Parameters=[
"username":textFieldUserName.text!,
"password":textFieldPassword.text!,
"name":textFieldName.text!,
"email":textFieldEmail.text!,
"phone":textFieldPhone.text!,
"user_type":String(user_type),
"user_privileges":String(user_privileges)
]
print("Post Contents ('parameters') = \(parameters)")
//Sending http post request
Alamofire.request(URL_USER_REGISTER, method: .post, parameters: parameters).responseJSON
{
response in
//printing response
print(response)
//getting the json value from the server
if let result = response.result.value {
//converting it as NSDictionary
let jsonData = result as! NSDictionary
}
}
Well, I feel sheepish. I found a hash being applied in the PHP on the server side.
md5($pass);
No need to bother with this one. Now wish I had not even posted it. But, maybe it will help someone.
I'm using RestKit v0.20.0-pre6 for iOS. My input is JSON from a service running on Google App Engine. This is what is being returned from the service:
{
action:"NOP",
payload:null,
timeStamp:new Date(1359427714679),
type:null
}
This is the error I'm getting displayed from RestKit in the output window:
E restkit.network:RKResponseMapperOperation.m:240
Failed to parse response data: Loaded an unprocessable response (200) with content type 'application/json'
It is choking when NSJSONSerialization is called to parse the data. I'm sure it is the line that contains new Date(1359427714679), but I am unsure on how to parse this line. The RestKit documentation mentions writing your own formatter, but I'm unclear on how to do this.
Any insight on how to solve this issue would be much appreciated.
You've got a couple of problems with your JSON here. Firstly, your keys need to be in inverted commas "". Secondly, your web service needs to provide the timestamp as a string.
Try ISO8601 format, e.g.
{
"action" :"NOP",
"payload" : null,
"timeStamp" : "1901-12-13T20:45:52+00:00",
"type": null
}
You can check that your JSON is valid by pasting it into an online validator such as http://www.jslint.com/.
I am making a POST request with RestSharp (on windows phone 7.1 client). I sent string to a service in a request body. Looks like the service is successfully called and it returns proper value (integer), however response object is null:
client.ExecuteAsync<T>(request, (response) => {
data = response.Data; // response is null in debugger
});
I cant understand why is that so.
<T> isn't a valid value for that call. I'm not sure that would even build there unless you've wrapped it in a generic method.
Also, is the response coming back as plain text? What's the Content-Type returned? Most likely, you should just use ExecuteAsync(request, callback) without the generic parameter and grab the data out of response.Content which is a string of the response body. response.Data is for the automatically deserialized XML or JSON (or custom) response if you use the generic method overload that specifies a type to deserialize to.
This seems to be an ongoing issue with RestSharp asynchronous calls - for HTTP transport errors ErrorException object is useless (returns null). Check the StatusCode property if it returns with anything but HttpStatusCode.OK. StatusDescription is not extremely useful either as it doesn't match complete status message from server response payload.
i'm working with a server which response using the JSON format.
when the request contain valid data they respond with a string like this
{"data":{"results":[{"Branch":"ACCT590006"}]}}
but if the parameters of the request are incorrect the response goes like this
{"error":{"errors":[{"domain":"global","reason":"invalid","message":"Invalid
Params"}],"code":98865,"message":"Invalid
param value"}}
So the questions are how i can determine when the response of the server contains a error string using the TJSONObject object and additionally parse the JSON string to show the messages and error codes like this.
Failed reason : invalid
Message : Invalid params
Code: 98865
message : invalid param value.
I've worked a little with JSON, an every time I've parsed from code(delphi 7). But i've searched a little bit, and here you may find the answer of your question:
http://edn.embarcadero.com/print/40882
and with a little adaption this should work.
Best regards,
Radu