【问题标题】:NSObject to json?NSObject 到 json?
【发布时间】:2011-12-24 00:57:50
【问题描述】:

我想将personArray 转换为JSON 字符串,并向服务器发送请求。

我尝试了类似以下代码:

@interface Person : NSObject {
    NSString *name;
    int registered;
}
+ (NSMutableArray *) select;
NSMutableArray *personArray = [Person select]; 


NSString *json = @"{ \"";//TODO

for (int i =0 ;i < [personArray count]; i++) {
    Person *temp = [Person objectAtIndex:i];

    [json stringByAppendingFormat:[NSString stringWithFormat:@"\"name\": \"%@\"", temp.name]
}
json = [json stringByAppendingFormat:[NSString stringWithFormat:@"} \""]];

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:[global userID] forKey:@"user_id"];
[request setPostValue:json forKey:@"json_key"];
[request addRequestHeader:@"Content-type" value:@"application/json"]; 

[request startSynchronous];

服务器收到以下数据:

{ \"\"name\": \"Tom\"}

服务器代码是这样的:

$json = $_POST['json_key'];
echo $json;
$json = json_decode($json, true);
echo $json; // prints nothing

有没有什么办法可以去掉斜线,或者有什么更漂亮的方法可以将对象转换成 JSON?

【问题讨论】:

    标签: php iphone objective-c ios json


    【解决方案1】:

    为确保正确生成 JSON 表示,请使用通用 JSON 生成器(例如 Stig Brautaset 的 JSON Framewarkyajl-objc)而不是临时转换。

    JSON 框架:

    @interface Person(SBJson)
    -(id)proxyForJson;
    @end
    
    @implementation Person(SBJson)
    -(id)proxyForJson {
    return [NSDictionary dictionaryWithObjectsAndKeys:
        name,@"name",
        [NSNumber numberWithInt:registered],@"registered",
         nil];
    }
    @end
    
    ...
        ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
        [request setPostValue:[[Person select] JSONRepresentation] forKey:@"json_key"];
    

    yajl-objc:

    @interface Person(YAJL)
    -(id)JSON;
    @end
    
    @implementation Person(YAJL)
    -(id)JSON {
    return [NSDictionary dictionaryWithObjectsAndKeys:
        name,@"name",
        [NSNumber numberWithInt:registered],@"registered",
         nil];
    }
    @end
    
    ...
        ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
        [request setPostValue:[[Person select] yajl_JSONString] forKey:@"json_key"];
    

    另见:

    【讨论】:

    • 谢谢,这正是我想要的!!
    【解决方案2】:

    在服务器上,您可能打开了magic_quotes_gpc。尝试更多类似的方法:

    $json = $_POST['json_key'];
    
    if (get_magic_quotes_gpc()) {
        $json = stripslashes($json);
    }
    
    echo $json;
    $json = json_decode($json, true);
    print_r($json); 
    

    【讨论】:

      【解决方案3】:
       NSString *json = @"{ \"";//TODO
      

      json = [json stringByAppendingFormat:[NSString stringWithFormat:@"} \""]];
      

      在我看来非常可疑。您在 json 字符串的开头添加了一个引号,这将导致第一个键前面有一个杂散的 "。最后的 " 看起来是假的。

      为什么不使用实际的 JSON 库(例如 touchjsonsbjson)而不是拼凑一些字符串?

      【讨论】:

        【解决方案4】:

        我很惊讶这给了你任何东西。首先,这条线……

        [json stringByAppendingFormat:[NSString stringWithFormat:@"\"name\": \"%@\"", temp.name]];
        

        ……什么都不做。 stringByAppendingFormat: 根据接收者返回一个新的字符串(即json),它不会修改接收者。此外,您正在创建一个具有格式的新字符串,以附加到 json 的格式;可以简化为:

        json = [json stringByAppendingFormat:@"\"name\": \"%@\"", temp.name];
        

        另外,与其不断创建新字符串并可能淹没自动释放池,不如改用NSMutableString,并使用appendFormat:appendString: 方法修改接收器而不是创建新字符串。

        正如其他人所提到的,JSON 可能无法正确解码的原因是它可能格式不正确,您的 JSON 字符串中有一个杂散的\"

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-17
          • 1970-01-01
          • 2013-08-20
          • 1970-01-01
          • 1970-01-01
          • 2021-10-21
          相关资源
          最近更新 更多