Tag: json

PHP / IOS:什么是最好的方式来编码JSON的Web服务?

我正在尝试创build一个Web服务,通​​过PHP在MySQL应用程序中显示从一个MySQL数据库提供JSON。 我有一个标准的MySQL / PHP设置。 数据在一个带有字段和logging的表格中。 用sql查询来创build一个logging集。 logging集中的每个logging都是一行。 php $sql = "SELECT userid,task,longtask FROM tasks WHERE userid = 1 LIMIT 1"; $res = mysql_query($sql) or die(mysql_error()); $tasks = array(); while($row = mysql_fetch_assoc($res)) { $tasks[] = array('row'=>$row); } echo json_encode(array('tasks'=>$tasks)); // Web服务产生以下输出: {"tasks":[{"row":{"userid":"1","task":"send email to Bob","longtask":"include attached memo"}}]} 然而,我很难把它读入IOS,提示可能有更好的Web服务格式。 这个结构不同于我在阅读json到IOS(其中没有使用php / mysql)的教程和其他资源中的结构。 任何人都可以告诉我一个更好的方法来构造JSON,或者在iOS中读取这个JSON来获取用户标识,任务和其他variables。 当我尝试这个,我得到一个错误,它不能放在行索引:0 NSError* error; NSDictionary* […]

如何防止JSONKit从ASP.NET JSONdate格式转义反斜杠?

我正在使用JSONKit在ASP.NET RESTful服务之间对JSON进行编码/解码。 服务使用的date格式在这里讨论,如下所示: "\/Date(1198908717056)\/" 问题是当JSONKit处理一个看起来像上面的string时,它会转义反斜杠,所以最终结果如下所示: "\\/Date(1198908717056)\\/" JSON规范说你可以select转义正斜杠(/),所以JSONKit应该解释“\ /”原样,而不是转义反斜杠。 有没有人知道一种方法来防止JSONKit逃避反斜杠时,正如上面的情况下的ASP.NET JSONdate格式的正斜杠?

Firebase检索数据 – 无法投射价值

首先,我已经检查了这些不帮我的答案: Swift JSON错误,无法将types'__NSArrayM'(0x507b58)的值转换为'NSDictionary'(0x507d74) 从Firebase获取数据 从Firebase(3.x)检索数据时,发生的错误是: Could not cast value of type '__NSArrayM' (0x10ca9fc30) to 'NSDictionary' (0x10caa0108). 用这个代码和树: 树: 检索function: func retrievePlanes() { print("Retrieve Planes") ref = FIRDatabase.database().reference(withPath: "results") ref.observe(.value, with: { snapshot in var newItems: [Planes] = [] for item in snapshot.children { let planesItem = Planes(snapshot: item as! FIRDataSnapshot) newItems.append(planesItem) } self.planes = newItems […]

如何检查值null并将其replace在NSDictionary中

现在,我正在使用NSUserDefault和NSDictionary,我保存在NSUserDefault的NSDictionary,不幸的是我不能,因为NSDictionary返回到Json有空值。 我需要检查,如果NSDictionary具有空值和replace。 怎么样? 这是NSDictionary, ({ ID:11, name:21-12-2012, des:"<null>", url: [ { ID:1, name: "<null>" }, { ID:2, name:"<null>" } ] }, { ID:12, name:if i die young, des:"<null>", url: [ { ID:3, name: "<null>" }, { ID:21, name:"<null>" } ] })

使用JSONKitparsingJSON文件

我正在构build一个音叉应用程序。 叉子应该允许12个预先设定的音高。 而且,我希望允许用户select一个主题。 每个主题将加载一组预设(不必使用它们全部)。 我的configuration文件看起来像这样*: theme: "A3" comment: "An octave below concert pitch (ie A4 440Hz)" presets: { A3 220Hz=220.0 } // http://en.wikipedia.org/wiki/Guitar_tuning theme: "Guitar Standard Tuning" comment:"EADGBE using 12-TET tuning" presets: { E2=82.41 A2=110.00 D3=146.83 G3=196.00 B3=246.94 E4=329.63 } theme: "Bass Guitar Standard Tuning" comment: "EADG using 12-TET tuning" presets: { E1=41.204 A2=55.000 D3=73.416 […]

使用JSONEncoder将nil值编码为null

我正在使用Swift 4的JSONEncoder 。 我有一个可选属性的Codable结构,我想这个属性显示为null值生成的JSON数据时,值nil 。 但是, JSONEncoder会丢弃该属性,并不会将其添加到JSON输出中。 有没有办法configurationJSONEncoder以便它保留的关键,并将其设置为null在这种情况下? 例 下面的代码片段产生{"number":1} ,但我宁愿它给我{"string":null,"number":1} : struct Foo: Codable { var string: String? = nil var number: Int = 1 } let encoder = JSONEncoder() let data = try! encoder.encode(Foo()) print(String(data: data, encoding: .utf8)!)

NSURLSessionUploadTask如何读取服务器响应

我正在使用NSURLSessionUploadTask来上传文件。 这里是我的代码的一些部分不完整 let session:NSURLSession = NSURLSession(configuration: config, delegate: self, delegateQueue: NSOperationQueue .mainQueue()) let sessionTask:NSURLSessionUploadTask = session.uploadTaskWithStreamedRequest(request 但问题是我无法获得服务器发回的JSON响应。 下面的代表也没有开火,但其他代表正在开火 func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) 我正在使用的代码: func sendFileToServer1(fileName:String,fileData:NSData,serverURL:String){ let body = NSMutableData() let mimetype = "application/octet-stream" // let mimetype = "video/quicktime" let boundary = "Boundary-\(NSUUID().UUIDString)" let url = NSURL(string: serverURL) let request = NSMutableURLRequest(URL: […]

如何将此string转换为json或ios中的字典

我厌倦了链接,但这些都不适合我的情况。 任何人都可以build议我如何将此string转换为JSON或字典。 这个string有一个string数组的字典。 NSString *temp=@"{\n name = {\n dob = \"\";\n age = \"61\";\n family = (\n {\n location = location;\n mobile = mobile;\n }\n );\n };\n}"; 这里的家庭是一个arrays。

XcodeparsingJson

所以这是我的代码从一个url获得一个后json数组 // SENDING A POST JSON NSString *post = [NSString stringWithFormat:@"plm=1"]; NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:@"http://muvieplus.com/testjson/test.php"]]; [request setHTTPMethod:@"POST"]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setHTTPBody:postData]; NSURLResponse *requestResponse; NSData *requestHandler = [NSURLConnection sendSynchronousRequest:request returningResponse:&requestResponse error:nil]; NSString *requestReply = [[NSString alloc] initWithBytes:[requestHandler […]

如何在iphone sdk中设置JSON数组?

我有一个login表单,用户只能使用有效的memberID和password 。 如果用户input正确的珐琅和密码,我得到的结果string包含用户在注册过程中创build的用户信息,如果密码错误,则显示status 400为结果string,结果string为json数组包含一个f的上述值,一件事是,如果成功login发生它给用户信息staus 200 ,我的需要是从数组中检索状态消息,我需要validation它的应用程序,如果login成功( status 200 )需要redirect到主页面; 如果是( status 400 ),则显示不成功的login消息。 我的代码:编辑 -(IBAction)_clicksbtnsignIN:(id) sender { [_txtmemberId resignFirstResponder]; [_txtpassword resignFirstResponder]; NSString *connectionstring = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"]]; if ([connectionstring length]==0) { UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"you are not connected to the internet" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil]; [alert show]; [alert release]; } else { //NSAutoreleasePool *pool […]