Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将对象转换为json格式-iOS_Ios_Json - Fatal编程技术网

将对象转换为json格式-iOS

将对象转换为json格式-iOS,ios,json,Ios,Json,您好,我正在尝试在iOS上将本地数据转换为json格式。 格式如下: { "answers": [ { "question_id": 2, "answer": "4", "question_instance_id": 146 }, { "question_id": 2, "answer": "4",

您好,我正在尝试在iOS上将本地数据转换为json格式。 格式如下:

{
    "answers": [
        {
            "question_id": 2,
            "answer": "4",
            "question_instance_id": 146
        },
        {
            "question_id": 2,
            "answer": "4",
            "question_instance_id": 147
        },
        {
            "question_id": 2,
            "answer": "4",
            "question_instance_id": 148
        },
        {
            "question_id": 3,
            "answer": "Hdhd",
            "question_instance_id": 149
        }
    ],
    "last_name": "Jd",
    "first_name": "Js",
    "survey_id": 41
}
我浏览了各种博客,他们在那里解释了json编码。但我仍然无法理解如何处理嵌套字典,将数据转换为json格式,如本例所示

非常感谢您的帮助。

简单地说:

  NSDictionary *entireJson = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSDictionary
中的逻辑与JSON相同。对于您的答案,您可以这样回答:

NSArray *answersArray = entireJSon[@"answers"];

请注意,确保您的json是有效的(在本例中是…

您需要为此使用
NSJSONSerialization
,并且您需要的将自动完成。请看链接。 谢谢


这里是链接wonderlich教程的30秒摘要,它可能会帮助一些人。干杯

您需要了解的关于json的所有信息,它甚至不会滚动显示:)


如果您熟悉iOS数组和字典,那么您可以可视化它们与JSON的关系。JSON只是数组和字典的编码,因此如果将数据匹配到与现有数据相关的结构中,JSON编码将是相同的

如果您将上述JSON粘贴到类似以下的JSON解析器中:

您可以看到底层结构:


包含4个键/值对的字典,键为:“答案”、“姓”、“名”和“调查id”。所有键的值都是原语、字符串或数字,除了第一个“answers”,它的值是一系列子字典,都有键:“question\u id”、“answer”和“question\u instance\u id”

用JSON解析器解析(iOS有半打),导航结果字典,该字典包含一个数组,该数组包含使用旧数组/字典访问方法的字典。如果所有其他方法都失败了,请查看之前关于此主题的1000个问题中的任何一个,并查看它们是如何回答的。虽然您的问题可以解释为询问如何转到另一个方向,但在这种情况下,答案是将数据放入适当的字典和数组中,并通过JSON序列化程序运行它们。请参阅前面的500个问题。如果您正确阅读了我的问题,我不会尝试解析它,而是尝试转换为上述格式。我面临的复杂问题是,它由嵌套的词典组成,这让我感到困惑。“适当的字典和数组”甚至我都可以说Nyways如果你把你最外层的字典和日志记录下来,并且日志看起来像上面那样,那么在NSLog中只有
()
字符,上面有
[]
,那么你就有了正确的结构。我找到了答案。问题是我不清楚json数据中的词汇和数组结构。现在已经清楚了,我可以把它转换成json格式。
#define exampleURL [NSURL URLWithString:\
 @"http://api.kivaws.org/v1/loans/search.json?status=fundraising"]
-(void)viewDidLoad { [super viewDidLoad]; [self _jsonGet]; }

-(void)_jsonGet
    {
    NSLog(@"I'm getting some JSON data from the net.");
    dispatch_async(dispatch_get_main_queue(),
        ^{
        NSData* dataFromNet = [NSData dataWithContentsOfURL:exampleURL];
        [self _jsonParse:dataFromNet];
        });
    }

-(void)_jsonParse:(NSData *)jdat
    {
    NSLog(@"I did seem to get the data .. now parsing" );
    NSError* error;
    NSDictionary* jdic = [NSJSONSerialization JSONObjectWithData:jdat
        options:kNilOptions
        error:&error];
    // do this NSLog(@"%@", jdic); to see the fields available

    NSArray* latestLoans = [jdic objectForKey:@"loans"];
    NSLog(@"loans.count: %d \n\n\n", latestLoans.count);
    NSDictionary *oneLoan = latestLoans[3];
    NSLog(@"loans[3]: %@ \n\n\n\n", oneLoan);

    NSLog(@"...name: %@ \n\n\n\n", [oneLoan objectForKey:@"name"] ); 
    NSLog(@"...sector: %@ \n\n\n\n", [oneLoan objectForKey:@"sector"] ); 
    }