junko76
junko76

Reputation: 3

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?

Upvotes: 0

Views: 196

Answers (5)

Amin Negm-Awad
Amin Negm-Awad

Reputation: 16650

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.

Upvotes: 0

Captain Buck
Captain Buck

Reputation: 689

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];
}       

Upvotes: 0

Jatin Patel - JP
Jatin Patel - JP

Reputation: 3733

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];
}

Upvotes: 0

junko76
junko76

Reputation: 3

 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

Upvotes: 0

Burhanuddin Sunelwala
Burhanuddin Sunelwala

Reputation: 5343

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"];
    

Upvotes: 0

Related Questions