0

我有与此类似的 JSON,带有许多 id 和 ref 键:

jSon = {
        "id":"node1",
        "name":"Languages",
        "rel":"",
        "children":1,
        "step":1,
        "path":1,
        "nodes":[
            {
                "id":"node2",
                "name":"Java",
                "rel":"Pure Object Oriented Prog",
                "children":1,
                "step":2,
                "path":2,
                "nodes":[
                    {
                        "id":"node3",
                        "name":"C#",
                        "rel":"Framework",
                        "children":0,
                        "step":3,
                        "path":3,
                        "nodes":[]
                    },{
                        "id":"node4",
                        "name":"C++",
                        "rel":"OOPS",
                        "children":0,
                        "step":3,
                        "path":4,
                        "nodes":[]
                    }]
            }]
    };

在Objective-C中,我怎样才能到达元素的父节点,其中键id和值是我们说的node4或其他的。

在 xcode 中,我试图解析来自MVCweb的 JSON 数据API

我的web.apiJSON 是这个

当我注释掉时Newtonsoft.Json.PreserveReferencesHandling.Objects,JSON 变得非常大,通常像 35MB。

当我使用PreserveReferencesHandling.Objects.net 放置idref键来重新发送对象时。

在 JSON 中旅行时,如果到达ref带有 value 的键asdfg,我必须使用 keyid和 value `asdf.

这可能Xcode吗?

4

1 回答 1

0

您需要进行深度优先或广度优先搜索,具体取决于您认为哪个会最快找到节点。这是您需要的代码的未经测试的尝试:

深度优先

- (id)nodeWithID:(NSString *)identifier inDict:(NSDictionary *)dict
{
    id node = nil;
    if ([dict[@"id"] isEqualToString:identifier])
    {
        node = dict;
    }
    else
    {
        // Traverse the inner nodes to perform a depth-first search
        for (NSDictionary *innerNode in dict[@"nodes"])
        {
            node = [self nodeWithID:identifier inDict:innerNode];
            // Break once a node is found
            if (node != nil)
            {
                break;
            }
        }
    }
    return node;
}

广度优先

- (id)nodeWithID:(NSString *)identifier inDict:(NSDictionary *)dict
{
    id node = nil;

    if ([dict[@"id"] isEqualToString:identifier])
    {
        node = dict;
    }
    else
    {
        // Perform a breadth-first search
        NSMutableArray *nextDictionaries = [[NSMutableArray alloc] initWithArray:dict[@"nodes"]];
        while (node == nil && [nextDictionaries count] > 0)
        {
            dict = nextDictionaries[0];
            if ([dict[@"id"] isEqualToString:identifier])
            {
                node = dict;
            }
            else
            {
                [nextDictionaries addObjectsFromArray:dict[@"nodes"]];
                [nextDictionaries removeObjectAtIndex:0];
            }
        }
    }

    return node;
}

* 编辑* 或者如果你想花哨并在以下位置创建一个类别NSDictionary

typedef NS_ENUM(NSUInteger, NSIdentifierSearchOptions) {
    NSIdentifierSearchOptionBreadthFirst = 0,
    NSIdentifierSearchOptionDepthFirst = 1
};

@interface NSDictionary (IdentifierSearch)

- (id)objectForKeyIdentifier:(NSString *)identifier options:(NSIdentifierSearchOptions)options;

@end

@implementation NSDictionary (IdentifierSearch)

- (id)objectForKeyIdentifier:(NSString *)identifier options:(NSIdentifierSearchOptions)options
{
    id object = nil;
    switch (options)
    {
        case NSIdentifierSearchOptionBreadthFirst:
        {
            object = [[self class] breadthFirstNodeWithID:identifier inDict:self];
            break;
        }
        case NSIdentifierSearchOptionDepthFirst:
        {
            object = [[self class] depthFirstNodeWithID:identifier inDict:self];
            break;
        }
    }
    return object;
}

+ (id)breadthFirstNodeWithID:(NSString *)identifier inDict:(NSDictionary *)dict
{
    id node = nil;

    if ([dict[@"id"] isEqualToString:identifier])
    {
        node = dict;
    }
    else
    {
        // Perform a breadth-first search
        NSMutableArray *nextDictionaries = [[NSMutableArray alloc] initWithArray:dict[@"nodes"]];
        while (node == nil && [nextDictionaries count] > 0)
        {
            dict = nextDictionaries[0];
            if ([dict[@"id"] isEqualToString:identifier])
            {
                node = dict;
            }
            else
            {
                [nextDictionaries addObjectsFromArray:dict[@"nodes"]];
                [nextDictionaries removeObjectAtIndex:0];
            }
        }
    }

    return node;
}

+ (id)depthFirstNodeWithID:(NSString *)identifier inDict:(NSDictionary *)dict
{
    id node = nil;
    if ([dict[@"id"] isEqualToString:identifier])
    {
        node = dict;
    }
    else
    {
        // Traverse the inner nodes to perform a depth-first search
        for (NSDictionary *innerNode in dict[@"nodes"])
        {
            node = [self depthFirstNodeWithID:identifier inDict:innerNode];
            // Break once a node is found
            if (node != nil)
            {
                break;
            }
        }
    }
    return node;
}

@end
于 2013-09-27T11:01:52.660 回答