简体   繁体   中英

How assign value to a JSON string?

I get this JSON object from my web service

0:  {
Username: "John"
Password: "12345"
Position: "Admin"
Job: "Developer"
ResourceJobId: 1
}

When I try to assign a value, for example, to Username JSON string, I get a semantic error:

id obj = [NSJSONSerialization JSONObjectWithData:data
                                                 options:NSJSONReadingAllowFragments
                                                   error:&error];
            for (NSDictionary *dictionary in array) {
                NSString *usernameString = @"";
//this line of code gives me an error: Expression is not assignable               
[dictionary objectForKey:@"Username"] = usernameString   ;

I know how get the JSON object with NSJSONSerialization class, but my target is how assign the value @"" to the JSON object. So how can I assign the value @"" to the Username JSON string?

While you are trying to fetched dictionary from array . it is not mutable dictionary so you need to created NSMutableDictionary while fetching data from Array . following code will help you to change username .

for (NSMutableDictionary *dictionary in array)
{
   NSMutableDictionary* dictTemp = [NSMutableDictionary dictionaryWithDictionary:dictionary];

   [dictTemp setObject:usernameString forKey@"Username"];

   [_arrayList replaceObjectAtIndex:index withObject:dictTemp];
}

Well your assignment operation is written wrong, it should be indeed like this:

  • If you need to get value from dictionary

     usernameString = [dictionary objectForKey:@"Username"]; 
  • If you want to set a value to your dictionary, first of all your dictionary should be NSMutableDictionary

     [dictionary setObject:usernameString forKey:@"Username"]; 
 NSData *data = [NSData dataWithContentsOfURL:url];
        NSError *error;
        id obj = [NSJSONSerialization JSONObjectWithData:data
                                                 options:NSJSONReadingAllowFragments
                                                   error:&error];

        if(!error && [obj isKindOfClass:[NSArray class]]){
            NSArray *array = (NSArray *)obj;
            Booking *booking = nil;
            for (NSMutableDictionary *dictionary in array) {
               NSString *usernameString = @"";
//this line of code gives me an error: Expression is not assignable               
[dictionary objectForKey:@"Username"] = usernameString;

Yes, is a compiler error

You need to have a mutabledictionary inorder to change the value. Try this

for (NSDictionary *dictionary in array) {
   NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionaryWithDictionary:dictionary];
   NSString *usernameString = @"";     
   [mutableDictionary setValue:@"" forKey:usernameString];
}       

Even the answers are correct, I want to add that you do not have to do this your own. NSJSONSerialization already has a solution for that. Simply pass as options one of these:

NSJSONReadingMutableContainers = (1UL << 0),
NSJSONReadingMutableLeaves = (1UL << 1),

when reading the JSON.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM