简体   繁体   中英

appending JSON array in iOS 5

So I have a JSON in the following format:

{
    "data": {
        "last_updated": 1342277036, 
        "stream": [
            {
                "action": {
                },
                "to": [
                ], 
                "from": [
                ],  
                "timestamp": 1342276421
            }, 
            {
                "action": {
                },
                "to": [
                ], 
                "from": [
                ],  
                "timestamp": 1342276421
            }, 
            {
                "action": {
                },
                "to": [
                ], 
                "from": [
                ],  
                "timestamp": 1342276421
            },

I then make another request and get the same response. I wanted to combine these two JSON, basically appending the new "stream" array that I have to the older one's. Here's what I've done so far that failed graciously:

NSMutableArray *streamArray = 
  [NSMutableArray arrayWithArray:[[streamDiskData valueForKey:@"data"] valueForKey:@"stream"]];
NSArray *newFeeds = [[jsonData valueForKey:@"data"] valueForKey:@"stream"];
//append the old JSON array with the new feeds stream
[streamArray insertObjects:newFeeds atIndexes:indexSet];

So streamDiskData here is the old JSON and jsonData is the new JSON. What am I doing wrong and how can I do this?

I can't see what your indexSet actually is, but try using something more like this:

NSMutableArray *streamArray = 
   [NSMutableArray arrayWithArray:[[streamDiskData valueForKey:@"data"] valueForKey:@"stream"]];
NSArray *newFeeds = [[jsonData valueForKey:@"data"] valueForKey:@"stream"];
//append the old JSON array with the new feeds stream
[streamArray addObjectsFromArray: newFeeds];

addObjectsFromArray is what you'd use to append the second array. insertObjects:atIndexes: is useful for inserting the newArray data at other locations, other than just the end of streamArray . But, your question said append , so I would suggest using addObjectsFromArray instead.

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