标签: nsjsonserialization

无法将NSDictionary值转换为json

我有NSDictionary值,我想将其转换为Json字符串,如:

{
    body = "10-feb";
    comments =     (
    );
    complete = 1;
    "created_at" = "2014-02-09T15:56:01Z";
    "due_at" = "2014-01-10 20:00:00 +0000";
    "due_on" = "2014-10-02";
    id = 439824;
    "notification_sent" = 0;
    "notify_user" = 0;
    "organization_id" = 972;
    participants =     (
    );
    reminders =     (
    );
    starred = 0;
    "updated_at" = "2014-02-09T15:56:01Z";
    "user_id" = 11129;
}
Run Code Online (Sandbox Code Playgroud)

我将它转换为json的方式是:

- (NSString*) jsonStringWithPrettyPrint:(BOOL) prettyPrint str:(NSDictionary *)str {
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:str
                                                       options:(NSJSONWritingOptions)    (prettyPrint ? NSJSONWritingPrettyPrinted : 0)
                                                         error:&error];

    if (! jsonData) …
Run Code Online (Sandbox Code Playgroud)

json objective-c nsdictionary ios nsjsonserialization

0
推荐指数
1
解决办法
395
查看次数

如何删除警告“从‘Any’到‘AnyObject’的条件转换总是成功”

我创建了一个类,当我尝试将对象转换为 AnyObject 时收到一条警告消息。警告是:“从‘Any’到‘AnyObject’的条件转换总是成功”如何从我的文件中删除此警告?

这是我的代码:

class WebServices
{
    class func getRequest( urlString: String, successBlock :@escaping (_ response :AnyObject)->Void, errorMsg:@escaping (_ errorMessage :String)->Void )
    {
        var request = URLRequest(url: URL(string: urlString)!)
        request.httpMethod = "GET"

        let task = URLSession.shared.dataTask(with: request) { (data, urlResponse, error) in

            DispatchQueue.main.async {
                if(error == nil)
                {
                    do {
// Here is the warning 
                        let responseJSON = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? AnyObject 
                        guard let _ = responseJSON else {
                            errorMsg("Some error has been occurred!")
                            return
                        }
                        successBlock(responseJSON!) …
Run Code Online (Sandbox Code Playgroud)

ios nsjsonserialization nsurlsession swift

0
推荐指数
1
解决办法
5267
查看次数

Json Serializable - 无效参数(输入):不能为空

我正在尝试在我的模型类中设置 JSON 值。但其返回错误为无效参数(输入):不能为空

用户帐户.dart

import 'package:json_annotation/json_annotation.dart';
part 'userAccount.g.dart';


@JsonSerializable(nullable: true,disallowUnrecognizedKeys:true,includeIfNull:true)
class UserAccount{
     String id;
     String name;
     String email;
     String profile_pic;
     String country;
     String phoneNumber;
     bool isNewUser;
     int role;
     int account_status;
     String token;
     DateTime token_expiry;
     String facebook_profile_url;
     String google_profile_url;
     String twitter_profile_url;
     int matchStixx_count;
     DateTime created_at;
     DateTime updated_at;


     UserAccount({
          this.id,
          this.name,
          this.email,
          this.profile_pic,
          this.country,
          this.phoneNumber,
          this.isNewUser,
          this.role,
          this.account_status,
          this.token,
          this.token_expiry,
          this.facebook_profile_url,
          this.google_profile_url,
          this.twitter_profile_url,
          this.matchStixx_count,
          this.created_at,
         this.updated_at
     });

     factory UserAccount.fromJson(Map<String,dynamic> json) => _$UserAccountFromJson(json);

     Map<String,dynamic> toJson() =>_$UserAccountToJson(this);

}

Run Code Online (Sandbox Code Playgroud)

UserAccount.g.dart

part of 'userAccount.dart';

UserAccount …
Run Code Online (Sandbox Code Playgroud)

json jsonserializer dart nsjsonserialization flutter

0
推荐指数
1
解决办法
4036
查看次数

Objective-C - "成功"不等于"成功"

基本要点:我从Web服务中获得了一些JSON以验证登录; 那部分有效.我将数组中的值拉入NSDictionary中; 那部分有效.我需要检查一个返回的值,以了解它是否成功.这就是它失败的地方.据我所知,它告诉我"成功"不等于"成功".

NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData: response options: NSJSONReadingMutableContainers error: &err];

NSString *result = [jsonArray valueForKey:@"result"];
NSLog(@"%@",result);

if ([result isEqual:@"success"]) {
Run Code Online (Sandbox Code Playgroud)

日志显示"结果"被设置为"成功",但似乎永远不会评估为真.

如果我手动设置"结果":

NSString *result = @"success";
Run Code Online (Sandbox Code Playgroud)

...它进入if语句就好了,所以看起来有些东西我错过了指向数据类型或类似的东西......我只是在这一点上还有其他的尝试.

我通常是一个Web开发人员,但我是iOS的新手,所以我的调试在xcode中仍然有点缺乏,但我熟悉一般逻辑等等.你们给我的任何帮助都会很棒!

编辑:

NSLog显示从Web服务返回的JSON:

2014-01-10 16:22:42.568 LoginTest[1640:70b] (
    {
        code = 1;
        fname = Joe;
        lname = Tests;
        result = success;
        token = 2555f13bce42b14cdc9e60b923bb2b20;
        vendornum = 50000000;
    }
)
Run Code Online (Sandbox Code Playgroud)

编辑 - 最终工作代码:

NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: response options: NSJSONReadingMutableContainers error: &err];

NSLog(@"jsonArray: %@", jsonArray);

NSString *result = [jsonArray[0] objectForKey:@"result"];
NSLog(@"%@",result); …
Run Code Online (Sandbox Code Playgroud)

objective-c nsjsonserialization

-1
推荐指数
1
解决办法
175
查看次数