Search code examples
iosjsonjson-api

extract an attribute of a specific type from JSON API


How do I extract the attributes of a specific type from a JSON API format string? I used NSJSONSerialization- but that extracts the attributes and puts it under included.attributes

..."included":[{"id":"","type":"name1","attributes":{...}},{"id":"","type":"form-data","attributes":{..}}]}

that serializes into:

included =     (
            {
        attributes =             {..};id = "";
        type = "name1";
    },
            {
        attributes =             {...};
        id = "";
        type = "name2";
    }
);
}

is there a way to extract values of attribute based on value of type?


Solution

  • i used the following code to extract what i needed:

        for (NSMutableArray* oneRow in attributes) {
        if([[oneRow valueForKey:@"type"] isEqualToString:@"name"]){
            formAttribute = [oneRow valueForKey:@"attributes"];
        }
    }
    

    i had hoped to find a method or predefined function that could do this-if there is, hope you add it as an answer.