json에서 NSDictionary에 텍스트 필드 데이터를 저장하려고합니다. 나는 이것을 위해 SBJson을 사용했습니다.
{
"fields":[
{
"textFields":[
{
"text":"Congratulations",
"textSize":"12"
},
{
"text":"Best Wishes",
"textSize":"15"
},
{
"text":"Test text",
"textSize":"10"
}
]
},
{
"imageFields":[
{
"image":"test1.jpg",
"width":"200",
"height":"100"
},
{
"image":"test2.jpg",
"width":"200",
"height":"100"
}
]
}
]
}
내 코드 :
-(void)readJson{
NSDictionary *jsonDict = [jsonString JSONValue];
NSDictionary *fieldsDict =[jsonDict valueForKey:@"fields"];
NSDictionary *textFieldsDict = [fieldsDict valueForKey:@"textFields"];
NSLog(@" Dictionary %@ ",textFieldsDict );
}
그러나 출력은 다음과 같습니다.
Dictionary (
(
{
text = Congratulations;
textSize = 12;
},
{
text = "Best Wishes";
textSize = 15;
},
{
text = "Test text";
textSize = 10;
}
),
"<null>"
)
사전에 두 개의 항목이 있고 하나는 null 인 것 같습니다. 세 개의 텍스트 필드 항목을 배열에 넣고 싶었습니다. 이 문제를 어떻게 해결할 수 있습니까?
I have corrected the json format and used NSJSONSerialization,
{"fields":
{"textFields":
[ {"text":"Congratulations", "textSize":"12"},
{"text":"Best Wishes", "textSize":"15"},
{"text":"Test text", "textSize":"10"}
],
"imageFields":
[ {"image":"test1.jpg","width":"200", "height":"100"},
{"image":"test2.jpg", "width":"200", "height":"100"}
]
}
}
-(void)readJson
NSError *e = nil;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&e];
NSDictionary *fields = [jsonDict objectForKey:@"fields"];
NSArray *textArray=[fields objectForKey:@"textFields"] ;
NSLog(@"--- %@",textArray );
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다