来自JSON feed的字典的NSMutableArray为关键iOS插入新对象

时间:2013-04-28 22:36:27

标签: ios json nsmutablearray nsdictionary nsmutabledictionary

我从JSON提要接收字典数组并将其分配给名为jsonArray的NSMutableArray,即:

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

Feed看起来像这样:

reports =     (
            {
        address = "The street";
        email = "alex@blah.co.uk";
        "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 = "alex@blah.co.uk";
        "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";
    };

我想要做的是为key插入一个新值,让我们说这个jsonArray:distance =“0.16km”但是我的大脑今晚不工作....

所以新的jsonArray应该看起来像上面提到的附加键值:

reports =     (
            {
        address = "The street";
        email = "alex@blah.co.uk";
        "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 = "alex@blah.co.uk";
        "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";
    };

基本上我有一个计算,它从feed获取long和lat,并计算出poi离我当前位置的距离然后我使用基于我想要的距离键的排序描述符对数组进行排序插入上面。我有这个代码只是插入到上面的现有的nsmutablearray

1 个答案:

答案 0 :(得分:1)

将反序列化的JSON数组作为NSArray(不是NSMutableArray)读取。 然后使用以下内容创建该数组的可变副本:

NSMutableArray *mutableArray = [originalArray mutableCopy];

然后将项目插入mutableArray。