I use the AFNetWorking3.0, and use a tool (Wireshark) to get the post datas (request), eg I want to post a parameters like #{"name": #"zlj"}, the wireshark can get the right Datas, I can see #{"name": #"zlj"}.
But when I use like this, NSDictionary *para = #{#"json": #{#"name": #"zlj", #"sex": #"1"}}, and then I use AFNetWorking post this parameters , the wireshark get my post datas like -----"json%5Bname%5D=zlj&json%5Bsex%5D=1"
So I could not understand why show "json%5Bname%5D=zlj&json%5Bsex%5D=1", why not show "#{#"json": #{#"name": #"zlj", #"sex": #"1"}}", Can somebody tell my?
I think what you need to do is, instead of sending the dictionary direct to server , convert it to JSON serialization and send the data by POST as below.
NSMutableDictionary *dictionnary = [NSMutableDictionary dictionary];
[dictionnary setObject:#"zlj" forKey:#"name"];
[dictionnary setObject:#"1" forKey:#"sex"];
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionnary options:kNilOptions error:&error];
Then send the above jsonData to server through POST method , and it will work.
Related
This is the situation: in Objective-C, I'm fetching JSON data from my server. I know for sure (and it won't change) that my JSON data only contains one JSON element named token that is a string. It will look like this :
{
"token": "ertvgbyhnujk45678CVBNkjuhgfvgb"
}
What's the way to just get the token string value? It's probably very easy but I'm totally new to Objective-C.
Use the following:
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];
Then get the token by using: dictionary[#"token"].
The error parameter in the method above can be nil because you said you're sure that it won't change and it'll always be JSON.
NSString *token = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil][#"token"];
My web service api (POST) has a parameter that takes a dictionary object like this
void MyMethod (Dictionary<string, List<string>> myDictionaryParam);
Basically, the keys are strings and the values are an array of strings.
How do i send this data from Objective C.
So far, i have tried the following.
NSMutableDictionary* dataDictionary = [[NSMutableDictionary alloc] init];
[dataDictionary setObject:personIds forKey:"firstKey"];
[dataDictionary setObject:personIds forKey:#"secondKey"];
NSDictionary* dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:dataDictionary, #"myDictionaryParam", nil];
NSError* seralizationError;
NSData* data = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&seralizationError];
The API gets the dictionary, but the dictionary has 0 key/value pairs.
Don't worry about how to post json data..I got that part covered. I am interested in knowing how to post an actual dictionary object from Objective C
I'm trying to get one value from JSON. JSON is located in NSString and it looks like this:
{"coord":{"lon":-122.38,"lat":37.57},"weather":[{"id":300,"main":"Drizzle","description":"Lekka mżawka","icon":"09d"}],"base":"stations","main":{"temp":304.74,"pressure":1017,"humidity":35,"temp_min":300.15,"temp_max":307.59},"visibility":16093,"wind":{"speed":6.7,"deg":250},"clouds":{"all":75},"dt":1437346641,"sys":{"type":1,"id":478,"message":0.0615,"country":"US","sunrise":1437311022,"sunset":1437362859},"id":5357155,"name":"Hillsborough","cod":200}
I'm interested in getting "temp". How should I do that?
Assuming your JSON string was stored as a NSString named JSONString:
NSError *error;
NSDictionary *keys = [NSJSONSerialization JSONObjectWithData:[JSONString dataUsingEncoding:NSUTF8StringEncoding]
options:NSJSONReadingMutableContainers
error:&error];
NSLog(#"temp = %#", keys[#"main"][#"temp"]); // temp = 304.74
To get the main sub item in weather, which is an array with multiple items, you should point out its index to tell the selector which object in the array is the one you are looking for. In this case, it's 0:
NSLog(#"weather = %#", keys[#"weather"][0][#"main"]); // weather = Drizzle
I have a query in my app that return an NSCFDictionary object with the format :
object = { (values...) }; This is the json object:
[JSONHTTPClient getJSONFromURLWithString:url completion:^(id json, JSONModelError *err)
And I need edit this. How can convert this to String for make the changes and then convert to NSCFDictionary again??
Thanks!
I couldn't find anything in JSONModel's documentation indicating how to create a NSMutableDictionary from the JSON you receive in the HTTP response. That would be the ideal way to handle this.
What you can do is this:
NSDictionary *immutableDict = (NSDictionary *)json;
NSMutableDictionary *mutableDict = [immutableDict mutableCopy];
mutableDict[#"keyToChange"] = #"the new value";
I am from php domain, and now learning ios coding. I want to make a view with one part showing user info, and a table showing friends details.
I can get the json Logged correctly.
My json looks like this:
{"Me":
{"username":"aVC",
"userID":1
},
"Friends":
[{"username":"Amm",
"userID":2
},...
]
}
Here is what I use.
NSError *error;
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSDictionary *json = (NSDictionary*)[NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSLog(#"jS: %#", json);
this works fine. I want to seperate the two sections (Me, and friends), and then use it to fill tables. Can someone throw some ideas?
NOTE: I am not using any frameworks. Just NSJSONSerialization.
Try adding this code after obtaining the json dictionary:
NSDictionary *me = [json objectForKey:#"Me"];
NSArray *friends = [json objectForKey:#"Friends"];
This should let you pull the information from the #"Me" and #"Friends" into separate variables.