如何在iOS中纠正此字符串格式错误 [英] How to rectify this string format error in ios

查看:71
本文介绍了如何在iOS中纠正此字符串格式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我实际的json响应:

This is my actual json response:

{
"x": {
"user": {
"id": 1,
"name": "a"
},
"startday": "2016-01-10",
"status": "New",
"total": 1,
"time_entries": [
{
"id": 766,
"project": {
"id": 3,
"name": "b"
},
"issue": {
"id": 4
},
"user": {
"id": 1,
"name": "c"
},
"activity": {
"id": 8,
"name": "d"
}
"hours": 1,
"comments": "",
"spent_on": "2016-01-10"
}
]
}
}

我动态创建了此响应,如下所示:

I created this response dynamically, like this:

NSString * newstring = [NSString stringWithFormat:@"{\\\"wk_time\\\":\%@\{\\\"user\\\":\%@\{\\\"id\\\":\%@\,\\\"name\\\":\\\"%@\\\"\%@\,\\\"startday\\\":\\\"%@\\\",\\\"status\\\":\\\"%@\\\",\\\"total\\\":\%@\,\\\"time_entries\\\":[{\\\"id\\\":766,\\\"project\\\":\%@\{\\\"id\\\":\%@\,\\\"name\\\":\\\"%@\\\"\%@\,\\\"issue\\\":\%@\{\\\"id\\\":\%@\%@\,\\\"user\\\":\%@\{\\\"id\\\":\%@\,\\\"name\\\":\\\"%@\\\"\%@\,\\\"activity\\\":\%@\{\\\"id\\\":\%@\,\\\"name\\\":\\\"%@\\\"\%@\,\\\"hours\\\":\%@\,\\\"comments\\\":\%@\,\\\"spent_on\\\":\\\"%@\\\"\%@\}",,,...];

然后,以规定的格式进行操作:

Then, do it in prescribed format :

NSString * newstring1 = [NSString stringWithFormat:@"\"@%@\"",newstring];
NSLog(@"%@",newstring1);

因此,响应为:

@"{\"x\":{\"user\":{\"id\":1,\"name\":\"b\"},\"startday\":\"2016-01-10\",\"status\":\"New\",\"total\":1.0,\"time_entries\":[{\"id\":766,\"project\":{\"id\":3,\"name\":\"a\"},\"issue\":{\"id\":4},\"user\":{\"id\":1,\"name\":\"b\"},\"activity\":{\"id\":8,\"name\":\"c\"},\"hours\":1.0,\"comments\":\"\",\"spent_on\":\"2016-01-10\"}]}}"

如果我这样静态地发布此回复:

If i post this response statically like this:

NSString * jsoncode =@"{\"x\":{\"user\":{\"id\":1,\"name\":\"b\"},\"startday\":\"2016-01-10\",\"status\":\"New\",\"total\":1.0,\"time_entries\":[{\"id\":766,\"project\":{\"id\":3,\"name\":\"a\"},\"issue\":{\"id\":4},\"user\":{\"id\":1,\"name\":\"b\"},\"activity\":{\"id\":8,\"name\":\"c\"},\"hours\":1.0,\"comments\":\"\",\"spent_on\":\"2016-01-10\"}]}}";
NSData *objectData = [jsoncode  dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:objectData options:NSJSONReadingMutableContainers error:&jsonError];

在这种情况下它被序列化.显示值.但是在这样动态创建时:

It gets serialized.shows the values.on this scenario.But on creating dynamically like this:

NSString * newstring = [NSString stringWithFormat:@"{\\\"wk_time\\\":\%@\{\\\"user\\\":\%@\{\\\"id\\\":\%@\,\\\"name\\\":\\\"%@\\\"\%@\,\\\"startday\\\":\\\"%@\\\",\\\"status\\\":\\\"%@\\\",\\\"total\\\":\%@\,\\\"time_entries\\\":[{\\\"id\\\":766,\\\"project\\\":\%@\{\\\"id\\\":\%@\,\\\"name\\\":\\\"%@\\\"\%@\,\\\"issue\\\":\%@\{\\\"id\\\":\%@\%@\,\\\"user\\\":\%@\{\\\"id\\\":\%@\,\\\"name\\\":\\\"%@\\\"\%@\,\\\"activity\\\":\%@\{\\\"id\\\":\%@\,\\\"name\\\":\\\"%@\\\"\%@\,\\\"hours\\\":\%@\,\\\"comments\\\":\%@\,\\\"spent_on\\\":\\\"%@\\\"\%@\}",,,...];
NSString * newstring1 = [NSString stringWithFormat:@"@\"%@\"",newstring];
NSError *error=nil;
NSData * objectData = [newstring1 dataUsingEncoding:NSUTF8StringEncoding];

日志显示数据值. 但是在这一步之后,

The log shows data values. But after this step,

NSDictionary * json= [NSJSONSerialization JSONObjectWithData:objectData options:0 error:&error];

序列化后,它显示空值.

After serialization it shows null values.

NSLog(@"%@",error);

在错误消息上显示此内容.

On error message it show this.

Error Domain = NSCocoaErrorDomain代码= 3840该操作无法 完全的. (可可错误3840.)"(JSON文本不是以数组或 对象和允许未设置片段的选项.)UserInfo = 0x7fe0faa73210 {NSDebugDescription = JSON文本不是以数组或对象开头,并且 选项以允许不设置片段.}

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x7fe0faa73210 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

转换为字典时.

可能是错误.如何纠正?请对此进行指导.谢谢您的帮助.在静态执行时,它起作用,但是在动态创建时,它显示错误吗?该如何纠正?

What might be the error.How to rectify it?Please need guidance on this.Thanks for the help in advance. On doing statically it works,but dynamically creating this it shows error? how to rectify it?

推荐答案

您错过了activity词典之后的 .我格式化了"JSON".

You missed the , after the activity dictionary. I formatted the `JSON'.

{  
   "x":{  
      "user":{  
         "id":1,
         "name":"a"
      },
      "startday":"2016-01-10",
      "status":"New",
      "total":1,
      "time_entries":[  
         {  
            "id":766,
            "project":{  
               "id":3,
               "name":"b"
            },
            "issue":{  
               "id":4
            },
            "user":{  
               "id":1,
               "name":"c"
            },
            "activity":{  
               "id":8,
               "name":"d"
            },
            "hours":1,
            "comments":"",
            "spent_on":"2016-01-10"
         }
      ]
   }
}

您需要检查一下是否有许多网站和附加组件.建议的是 https://jsonformatter.curiousconcept.com/

You need to check at your end there are many websites and add-ons. Suggested one is https://jsonformatter.curiousconcept.com/

Chrome扩展名为:JSON编辑器

Chrome extention is: JSON Editor

这篇关于如何在iOS中纠正此字符串格式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆