Alex McPherson
Alex McPherson

Reputation: 3195

NSMutableArray of dictionaries from JSON feed insert new object for key iOS

I receive and array of dictionaries from a JSON feed and assign it to an NSMutableArray called jsonArray i.e:

jsonArray = [deserializedData objectForKey:@"reports"];

The feed looks like this:

reports =     (
            {
        address = "The street";
        email = "[email protected]";
        "eng_id" = 1;
        "eng_name" = "Alex McPherson";
        "eng_thumb" = "http://someurl/image/1.png";
        form = Test;
        id = 59;
        lat = "51.1438330";
        live = 1;
        lng = "0.8693330";
        location = "17 Victoria Crescent, Ashford, TN23 7HL";
        name = "Alex McPherson";
        phone = 01233000000;
        rid = "A5C963-C95B-C3D639";
        title = "#A5C963-C95B-C3D639, Litter";
        tm = "2013-04-28 20:44:20";
        type = 5;
        "type-text" = "Litter";
    },
            {
        address = "The street";
        email = "[email protected]";
        "eng_id" = 2;
        "eng_name" = "Rob Burt";
        "eng_thumb" = "http://someurl/image/1.png";
        form = Test;
        id = 122;
        lat = "51.1415000";
        live = 1;
        lng = "0.8715000";
        location = "38 Beaver Road, Ashford, TN23 7RP";
        name = Alex;
        phone = 01233000000;
        rid = "5A5C96-9072-6BAFA9";
        title = "#5A5C96-9072-6BAFA9, Litter";
        tm = "2013-04-28 20:35:56";
        type = 8;
        "type-text" = "Litter";
    };

what I would like to do is insert a new value for key lets say into this jsonArray: distance = "0.16km" but my brain is just not working tonight....

so the new jsonArray should looks like this with the added key value mentioned above:

reports =     (
            {
        address = "The street";
        email = "[email protected]";
        "eng_id" = 1;
        "eng_name" = "Alex McPherson";
        "eng_thumb" = "http://someurl/image/1.png";
        form = Test;
        id = 59;
        lat = "51.1438330";
        live = 1;
        lng = "0.8693330";
        distance = "0.16km";
        location = "17 Victoria Crescent, Ashford, TN23 7HL";
        name = "Alex McPherson";
        phone = 01233000000;
        rid = "A5C963-C95B-C3D639";
        title = "#A5C963-C95B-C3D639, Litter";
        tm = "2013-04-28 20:44:20";
        type = 5;
        "type-text" = "Litter";
    },
            {
        address = "The street";
        email = "[email protected]";
        "eng_id" = 2;
        "eng_name" = "Rob Burt";
        "eng_thumb" = "http://someurl/image/1.png";
        form = Test;
        id = 122;
        lat = "51.1415000";
        live = 1;
        lng = "0.8715000";
        distance = "2.13km";
        location = "38 Beaver Road, Ashford, TN23 7RP";
        name = Alex;
        phone = 01233000000;
        rid = "5A5C96-9072-6BAFA9";
        title = "#5A5C96-9072-6BAFA9, Litter";
        tm = "2013-04-28 20:35:56";
        type = 8;
        "type-text" = "Litter";
    };

basically I have a calculation that takes the long and lat from the feed and works out how far the poi is from my current location of which then I sort the array by using a sort descriptor based on the distance key that I want to insert above. I have the code for this just stuck on inserting into the existing nsmutablearray above

Upvotes: 1

Views: 762

Answers (1)

melsam
melsam

Reputation: 4977

Read the deserialized JSON array as an NSArray (not NSMutableArray). Then create a mutable copy of that array using something like:

NSMutableArray *mutableArray = [originalArray mutableCopy];

Then insert items into mutableArray.

Upvotes: 1

Related Questions