这是简化的 JSON 文件,我需要从网络服务下载它并在表格中解析结果!
编辑:我现在提供更精确的代码,通过在线工具清理和格式化:
{
"main": [
{
"id": 0, <--- float value
"type": "type0", <--- STRING value
"valueA": {
"valueA1": 1, <--- float value
"valueA2": 2, <--- float value
"valueA3": 3 <--- float value
},
"valueB": {
"valueB1": 1, <--- float value
"valueB2": 2 <--- float value
},
"valueC": [
{
"valueC1": "string0C1", <--- STRING value
"valueC2": 2, <--- float value
"valueC3": 3, <--- float value
}
]
},
通过在线工具 jsonviewer.stack.hu 格式化:
我需要用 AFJSONRequestOperation 解析它,我写了这段代码:
NSMutableArray *main = [JSON objectForKey:@"main"];
arrayID = [main valueForKey:@"id"];
arrayType = [main valueForKey:@"type"];
NSMutableArray *arrayValueC = [main valueForKey:@"valueC"];
NSMutableString *stringC1 = [arrayValueC valueForKey:@"valueC1"];
// I CANT USE objectForKey, XCode give an exception -[__NSArrayI objectForKey:]: unrecognized selector sent to instance
NSLog(@"id: %@",arrayID);
NSLog(@"type: %@",arrayType);
NSLog(@"string: %@",stringC1);
当我解析时,我从 NSLog 得到这个结果:
id: (
0,
1,
2,
3,
4,
5,
6,
7,
8,
9
)
type: (
type0,
type1,
type2,
type3,
type4,
type5,
type6,
type7,
type8,
type9
)
string: (
(
"string0C1"
),
(
"string2C1"
),
(
"string2C1"
),
(
"string3C1"
),
(
"string4C1"
),
(
"string5C1"
),
(
"string6C1"
),
(
"string7C1"
),
(
"string8C1"
),
(
"string9C1"
)
)
正如您所看到的那样,它非常完美,我可以推断 ID(浮点数)和 TYPE(字符串)的每个值,但我讨厌 valueC1 字符串的每个对象中的圆括号:我怎样才能得到没有括号和引号的干净 valueC1 ? 请如果你能提供一些代码。谢谢!