Order mismatch from RestAssured Response object to JSON conversion - rest-assured

I got a Rest Assured Response object. While trying to convert from Response object to JSON. The conversation happen successfully but JSON order is mismatch. It would be great if some one assist on this.
Conversion:
JSONObject jobj = new JSONObject(restAssuredResponse.getBody());

Basically, JSON is an unordered set of key value pairs. Which, for any JSON object the order of key/value pairs shouldn't be considered.
The JSON Object should be considered equal even if the order is not the same.

Related

Save a large JSON to Realm using Swift 3

I have a JSON with more or less 75 keys.
I need to receive this JSON and store offline it using Realm.
I do not want to iterate through the keys, since I've heard that there are ways to save a large JSON using a few lines. How can I do this?
EDIT:
My JSON (
I saved on a server away because it's too big)
http://myjson.com/i7e6l
There is no easy, one liner to parse the JSON and store it in Realm, since each JSON response is unique and no framework can have explicit knowledge about the structure of your JSON response without you giving some information to this framework about your JSON.
You will need to write some code either to parse the response or to make a mapping between your JSON response's fields and the properties of your Realm object. If you choose the latter solution, you can use Alamofire Object Mapper to do the JSON parsing automatically, but even then you have to write code for the mapping.

IOS/Objective-C/JSON:Obtain single value from Web Service in JSON

I want to obtain a single value from a web service in JSON, just a file name i.e. "picture.png"; based on some parameters passed.
Can IOS (I guess NSJSONSerialization JSONObjectWithData:) handle this single value in JSON or on the server side should I have it send a dictionary key as in {"pic": "picture.gif"}
If there is no picture, I am returning "nopic" so again should I have it return "error" or {"error": "nopic"}
I gather the various JSON specifications are conflicting on this point so my interest is just practical...how best to handle this case.
Thanks for any guidance on this

Rearrange the order of keys in NSDictionary for crypto hash match of entire dictionary

Here is what I'm trying to achieve.
I have a payload in the below format.
//Ordered Payload - observed at the receiving end.
{
"xx":"First",
"yy":"Second",
"11":{
"xx1":"innerVal1",
"xx2":"innerVal2"
}
}
Since NSDictionary is basically unordered, it kinda shuffles this key - value pairs with in the dictionary when viewing on console of Xcode.
The same payload order is captured(as above ordered dict) when making a POST request at the api proxy end.
To ensure that the payload isn't tampered I need to do a hash(HMAC) of the payload and attach it in the request. The problem is that when I hash the payload (converting NSDict to JSON string) as is unordered just as NSDict property, and it won't match at the proxy when it tries to compares the hash with that of the ordered payload received in the request.
When I do a 'po' on NSDictionary this is what I see and its the same every time.
//unOrdered Payload - observed at the sending end.
//observe that the key holding a dictionary is always seen first in the dictionary followed by the other key-value pairs.
{
"11":{
"xx2":"innerVal2",
"xx1":"innerVal1"
},
"yy":"Second",
"xx":"First"
}
Is there any way I could make sure that my hash's are the same when the payload is not tampered ?
I understand its clearly not so possible, I think its worth giving a shot.
I have come up with other logic to use directly the key-value pairs for hashing and not the complete dictionary.
But since the payload format could be changing over the time, I don't want to keep changing the code at both client and server side for it.
Appreciate your time to help me find a solution for it.
Just hash the JSON data prior to sending, not the dictionary.
Convert the NSDictionary to JSON data with NSJSONSerialization method: dataWithJSONObject.
Hash the JSON data.
Sent the JSON data and hash.
On the server side:
Hash the received JSON data
Compare the computed hash to the received hash value
If hasing the JSON data is not an option just hash all the keys and values together or perhaps just the values if the keys are not an issue. Sort the keys and use that key order for hashing order.
Or just use SSL.

How to retain order of JSON data retrieved with AFNetworking?

I am using AFNetworking to retrieve JSON data from a web service. Part of the response string I get is:
{"DATA":{"LEASE TYPE":"3 Yrs + 0 renew of 0 Yrs","LANDLORD":"","TENANT":"test comp"...
and so on. The order of the key values in the "DATA" dictionary ("LEASE TYPE","LANDLORD","TENANT"...) is important for presentation purposes. However, when AFNetworking calls NSJSONSerialization's:
+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error;
method, the returned dictionary has the keys in a different order.
I notice that the AFJSONRequestOperation object has the server's response stored as an NSString, with everything in the correct order. However I'm not keen on parsing the JSON by hand if I can avoid it.
Can anyone suggest a way that will let me get at / keep the keys in their original order?
Thanks.
If the order is important use an array not a dictionary, dictionaries are be by their nature unordered. Or add an array of dictionary keys in the order desired.
If you have no control over the response that is sent you will have to parse the JSON yourself at least for the ordering.
When you'r creating an NSDictionary, the order will not be the same. I often recognized that dictionaries get ordered by key-name alphabetically.
But when using dictionaries the order doesn't really matter. And they shouldn't!
As the previous answers mentions dictionaries are by nature without order, but you can find here a nice class of OrderedDictionary:
http://www.cocoawithlove.com/2008/12/ordereddictionary-subclassing-cocoa.html
http://projectswithlove.com/projects/OrderedDictionary.zip

Traverse through JSon data in dart?

After trying a lot sorry for asking such a trivial question.
Given below screenshot consist of a data that I have successfully received from the server.
I would like to know how to traverse through the data since whenever I try to cast it to something and try a foreach it gives an error.
The actual data sent from the server is a List() type.
I want to know how to cast it to same type and use it here.
I tried casting but it says unexpected token here.
Any help is appreciated.
JSON.parse return type depends on the String you try to parse. See the doc :
Parses json and build the corresponding parsed JSON value.
Parsed JSON values are of the types num, String, bool, Null, Lists of parsed JSON values or Maps from String to parsed JSON values.
From your screenshot, it seems that the return value is a List. You can do something like the following to use it (did you notice the typo in your commented code - .fore) :
final parsedList = JSON.parse(e.data)/*.fore*/;
parsedList.forEach((x){
query('#idData').appendText(x[0]);
query('#idData').appendText(x[1]);
query('#idData').appendText(x[2]);
query('#idData').appendText(x[3]);
});

Resources