2010-07-14 5 views
4

내 프로젝트에서 JSON 프레임 워크를 사용하여 JSON 송신을 서버에서 성공적으로 디코딩했습니다.NSMutableArray를 JSON- 프레임 워크를 사용하여 JSON으로 보냄

이제는 다른 방법으로 처리해야하는데 보낼 데이터가 CoreData에서 가져온 NSMutableArray이므로 문제가 발생합니다.

NSString* jsonString = [menuItems JSONRepresentation] 

를 사용하여 나는 "JSON 직렬화 메뉴 아이템에 대해 지원되지 않습니다"라는 메시지를 얻을.

NSMutableArray를 JSON Framework에서 직렬화 할 수 있도록 다른 형식으로 변환해야합니까? 어떤 도움

감사합니다,
미구엘

답변

1

나는 마침내 그것을 해결,하지만 난 그것을 할 수있는 최선의/가장 우아한 방법이 있는지 확실하지 않다.

NSMutableArray* items = [[NSMutableArray alloc] init]; 
for (MenuItems* item in menuItems) { 
    [items addObject:[NSArray arrayWithObjects:item.id,[item.modified description],nil]]; 
} 
NSString *post = [NSString stringWithFormat:@"currentData=%@", 
        [items JSONRepresentation]]; 

설명 :
내가 먼저 문제가있는 NSMutableArray라고 생각하지만 그것의 내용 것을 깨달았다. -proxyForJson을 구현하여 메뉴 아이템 클래스에서

: 그래서 난 그냥 그것에서 필요한 정보를 얻을 수 및 JSON-프레임 워크

4

나를 다소 더 좋은 솔루션을 제안 할 수 있도록 허용 :-)에게 동의하지있는 NSArray로 저장 메서드를 호출하면 menuItems 배열에서 직접 -JSONRepresentation 메서드를 호출 할 수 있습니다.

@interface MenuItems(SBJson) 
-(id)proxyForJson { 
    return [NSDictionary dictionaryWithObjectsAndKeys: 
      self.id,@"id", 
      [self.modified description],@"modified", 
      nil]; 
} 
@end 

희망이 있습니다.

+0

안녕 Stig, 나는 그것을 시도 할 것이다 ... 감사합니다 :-) – Michi

+0

당신은 그것을 작동 시키셨습니까? –

+0

안녕하세요 Stig, 아직 테스트 할 시간을 찾지 못했습니다 ... 시도해 보니 곧 알려 드리겠습니다. – Michi

0

이것은 1000000 % 나를 위해 일한 서버에 사전과 배열을 보내는 예입니다.

SBJSON *jparser = [[SBJSON new] autorelease]; 


NSString *ArrayjsonItems = [jparser stringWithObject:self.UrMergedArray]; 

NSString *DicjsonItems = [jparser stringWithObject:self.UrMergedDic]; 




NSLog(@"array Items :%@",self.UrMergedArray); 

NSLog(@"dic Items :%@",self.UrMergedDic); 




NSString *postString =[NSString stringWithFormat:@"Arrayitems=%@&Dicitems=%@",ArrayjsonItems,DicjsonItems]; 


NSLog(@"it is going to post : %@ \n\n",postString); 



NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:snapURL]; 

[request setHTTPMethod:@"POST"]; 

[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]]; 



NSURLConnection *connection=[[NSURLConnection alloc] 
          initWithRequest:request 
          delegate:self]; 


if (connection) { 

    self.receivedData = [[NSMutableData alloc]init]; 

} 
+0

SBJSON 클래스는 _oooold_ 릴리스에 있습니다. SBJson 사본을 업그레이드하십시오! –

관련 문제