nsjsonserialization

JSON parsing using NSJSONSerialization in iOS

≡放荡痞女 提交于 2019-11-27 20:54:48
I am parsing a JSON in my code. But I am getting some unexpected issues while retrieving data of parsed JSON . So let me explain my problem. I have to parse following JSON data using xcode. This is what data to be parsed looks like while I hit same URL in browser: { "RESPONSE":[ {"id":"20", "username":"john", "email":"abc@gmail.com", "phone":"1234567890", "location":"31.000,71.000"}], "STATUS":"OK", "MESSAGE":"Here will be message" } My code to reach up to this JSON data is as follow: NSData *data = [NSData dataWithContentsOfURL:finalurl]; NSError *error; NSDictionary *json =

Newtonsoft inline formatting for subelement while serializing

风流意气都作罢 提交于 2019-11-27 16:22:58
Is it possible to create an attribute to serialize some subelements inline (Formatting.None) with newtonsoft json.net? I have a very huge set of data and I want to keep it readeable. Some subelements are not very important and can be writen inline. { "name": "xxx", "desc": "xxx", "subelem": [ {"val1": 1, "val2": 2, ...}, //inline, {"val1": 1, "val2": 2, ...}, ... ] "subelem2": { "val1": 1, "val2": 2, ... } } I want to force the inline serialization for some sub objects of my models. In this case, "subelem" items will be written inline. Thanks Adding the converter as a JsonConverterAttribute on

Swift: Convert struct to JSON?

青春壹個敷衍的年華 提交于 2019-11-27 16:02:20
I created a struct and want to save it as a JSON-file. struct Sentence { var sentence = "" var lang = "" } var s = Sentence() s.sentence = "Hello world" s.lang = "en" print(s) ...which results in: Sentence(sentence: "Hello world", lang: "en") But how can I convert the struct object to something like: { "sentence": "Hello world", "lang": "en" } You could add a computed property to get the JSON representation and a static (class) function to create an JSON array from a Sentence array. struct Sentence { var sentence = "" var lang = "" static func jsonArray(array : [Sentence]) -> String { return "

How can you deserialize an escaped JSON string with NSJSONSerialization?

ε祈祈猫儿з 提交于 2019-11-27 13:41:09
I have an iOS app that needs to process a response from a web service. The response is a serialized JSON string containing a serialized JSON object, looking something like this: "{ \"name\" : \"Bob\", \"age\" : 21 }" Note that this response is a JSON string , not a JSON object. What I need to do is deserialize the string, so that I get this: { "name" : "Bob", "age" : 21 } And then I can use +[NSJSONSerialization JSONObjectWithData:options:error:] to deserialize that into an NSDictionary . But, how do I do that first step? That is, how to I "unescape" the string so that I have a serialized JSON

NSJSONSerialization from NSString

扶醉桌前 提交于 2019-11-27 12:34:32
Is it possible if I have a NSString and I want to use NSJSONSerialization? How do I do this? First you will need to convert your NSString to NSData by doing the following NSData *data = [stringData dataUsingEncoding:NSUTF8StringEncoding]; then simply use the JSONObjectWithData method to convert it to JSON id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; You need to convert your NSString to NSData , at that point you can use the +[NSJSONSerialization JSONObjectWithData:options:error:] method. NSString * jsonString = YOUR_STRING; NSData * data = [jsonString

JSON.NET serialize JObject while ignoring null properties

一世执手 提交于 2019-11-27 08:55:10
I have a JObject which is used as a template for calling RESTful web services. This JObject gets created via a parser and since it's used as a template telling the user what the endpoint schema looks like, I had to figure out a way to preserve all properties, which is why I'm defaulting their values to null . As as example, this is what the object originally looks like: { "Foo":{ "P1":null, "P2":null, "P3":null, "P4":{ "P1":null, "P2":null, "P3":null, }, "FooArray":[ { "F1":null, "F2":null, "F3":null, } ] }, "Bar":null } The user is then able to fill in individual fields as they need, such as

NSJSONSerialization serialization of a string containing forward slashes / and HTML is escaped incorrectly

[亡魂溺海] 提交于 2019-11-27 03:23:30
问题 I am trying to convert some simple HTML into a string value in a JSON object and I'm having trouble getting the string encoding to not escape the string in NSJSONSerialization. Example... I have a string which contains some basic HTML text: NSString *str = @"<html><body><p>Samples / Text</p></body></html>"; The desired outcome is JSON with HTML as the value: { "Title":"My Title", "Instructions":"<html><body><p>Samples / Text</p></body></html>" } I'm using the standard technique to convert an

NSJSONSerialization results in EXC_BAD_ACCESS

醉酒当歌 提交于 2019-11-27 02:57:42
问题 Currently I am writing an app (Target iOS 6, ARC enabled) that uses JSON for data transmission and Core Data for persistent storage. The JSON data is generated out of a MySQL database by a PHP script via json_encode. My Problem is that with data from certain tables the following code fails: - (NSDictionary *)executeFetch:(NSString *)query { NSURL *requesturl = [NSURL URLWithString:[query stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; NSError *dataError = nil; self.jsonData

How can you deserialize an escaped JSON string with NSJSONSerialization?

混江龙づ霸主 提交于 2019-11-26 18:19:46
问题 I have an iOS app that needs to process a response from a web service. The response is a serialized JSON string containing a serialized JSON object, looking something like this: "{ \"name\" : \"Bob\", \"age\" : 21 }" Note that this response is a JSON string , not a JSON object. What I need to do is deserialize the string, so that I get this: { "name" : "Bob", "age" : 21 } And then I can use +[NSJSONSerialization JSONObjectWithData:options:error:] to deserialize that into an NSDictionary . But

Swift: Convert struct to JSON?

时光毁灭记忆、已成空白 提交于 2019-11-26 17:23:09
问题 I created a struct and want to save it as a JSON-file. struct Sentence { var sentence = "" var lang = "" } var s = Sentence() s.sentence = "Hello world" s.lang = "en" print(s) ...which results in: Sentence(sentence: "Hello world", lang: "en") But how can I convert the struct object to something like: { "sentence": "Hello world", "lang": "en" } 回答1: You could add a computed property to get the JSON representation and a static (class) function to create an JSON array from a Sentence array.