需要帮助来解析复杂的JSON
问题描述:
我试图解析一个复杂的json(使用Json框架),问题是json有很多嵌套的对象。花了太多的时间编程,我找不到解决方案,我的代码闻起来,我有很多崩溃。 有什么想法? (我没有任何问题使用另一个JSON框架,如jsonkit或其他)需要帮助来解析复杂的JSON
这里是json的简化版本。
[
{
"category": {
"id": 451,
"description": "Product description",
"quantity": "3149",
"category": [
{
"category": {
"id": 115,
"description": "Product description",
"quantity": "465",
"category": [
{
"category": {
"id": 125,
"description": "Product description",
"quantity": "6"
}
},
{
"category": {
"id": 120,
"description": "Product description",
"quantity": "57",
"category": [
{
"category": {
"id": 193,
"description": "Product description",
"quantity": "27"
}
},
{
"category": {
"id": 194,
"description": "Product description",
"quantity": "14"
}
}
]
}
},
{
"category": {
"id": 446,
"description": "Product description",
"quantity": "7"
}
},
{
"category": {
"id": 132,
"description": "Product description",
"quantity": "28",
"category": [
{
"category": {
"id": 224,
"description": "Product description",
"quantity": "3"
}
},
{
"category": {
"id": 137,
"description": "Product description",
"quantity": "4"
}
}
]
}
},
{
"category": {
"id": 128,
"description": "Product description",
"quantity": "21"
}
}
]
}
},
{
"category": {
"id": 21,
"description": "Product description",
"quantity": "225",
"category": [
{
"category": {
"id": 170,
"description": "Product description",
"quantity": "1"
}
},
{
"category": {
"id": 68,
"description": "Product description",
"quantity": "45",
"category": [
{
"category": {
"id": 81,
"description": "Product description",
"quantity": "2"
}
},
{
"category": {
"id": 69,
"description": "Product description",
"quantity": "2"
}
}
]
}
}
]
}
}
]
}
},
{
"category": {
"id": 390,
"description": "Product description",
"quantity": "251",
"category": [
{
"category": {
"id": 714,
"description": "Product description",
"quantity": "0"
}
},
{
"category": {
"id": 622,
"description": "Product description",
"quantity": "22",
"category": [
{
"category": {
"id": 192,
"description": "Product description",
"quantity": "16"
}
},
{
"category": {
"id": 391,
"description": "Product description",
"quantity": "6"
}
}
]
}
},
{
"category": {
"id": 612,
"description": "Product description",
"quantity": "5"
}
},
{
"category": {
"id": 621,
"description": "Product description",
"quantity": "123",
"category": [
{
"category": {
"id": 628,
"description": "Product description",
"quantity": "25"
}
},
{
"category": {
"id": 457,
"description": "Product description",
"quantity": "3"
}
}
]
}
},
{
"category": {
"id": 720,
"description": "Product description",
"quantity": "1"
}
},
{
"category": {
"id": 188,
"description": "Product description",
"quantity": "52",
"category": [
{
"category": {
"id": 437,
"description": "Product description",
"quantity": "10"
}
},
{
"category": {
"id": 639,
"description": "Product description",
"quantity": "7"
}
},
{
"category": {
"id": 152,
"description": "Product description",
"quantity": "4",
"category": [
{
"category": {
"id": 34,
"description": "Product description",
"quantity": "3"
}
},
{
"category": {
"id": 31,
"description": "Product description",
"quantity": "1"
}
}
]
}
}
]
}
},
{
"category": {
"id": 548,
"description": "Product description",
"quantity": "3"
}
}
]
}
}]
我的目的是创建对象数组来填充数据库,表看起来是这样的:
idfather(父类)
idproduct
描述
量
时间戳
答
你可以使用TouchJSON,它非常简单并且有很好的表现。它将你的json作为数据,并且可以在一行中返回一个NSArray或NSDictionnary。
如果你的JSON有一个列表结构:
NSArray *array = [[CJSONDeserializer deserializer] deserializeAsArray:yourData error:nil];
否则:
NSDictionary *dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:yourData error:nil;
你一定要包括你想使用它的类项目中的ANS进口#import "CJSONDeserializer.h"
TouchJSON
。 https://github.com/TouchCode/TouchJSON
答
看起来您的问题可能是JSON数据结构中的列表。当我使用JSON框架时,我无法使用keypath访问数组值。
我解决了服务器端的问题,不允许数据结构中的数组 - 只有键/值对。然后,我可以通过根节点的keypath访问结构中的每个值。 (当我想将列表转换为JSON时,我只使用了从0开始的数字。)
你确定数据结构的语法是正确的吗?也许你的框架无法处理它 - 尽管它应该能够。我使用这个:https://github.com/stig/json-framework/
你没有告诉我们你的问题到底是什么细节。它在哪里崩溃?是什么原因?从json-framework返回的信息处理这些信息的代码是什么样的?所有这些都是需要回答的有效问题,任何人都可以给你任何可靠的建议。 – jer 2011-03-23 15:43:13
JSON示例不是非常复杂。任何JSON库都应该能够处理它。所以问题不在于解析JSON,问题可能在于你走过的结果对象。但是没有一些我们永远不会知道的代码。 – 2011-03-23 16:02:02
键值编码呢?它基本上解决了我所有关于漫步JSON数据的问题。 – 2012-01-17 17:16:34