Remove escaping of quotes with Newtonsoft.Json - iphone

I have a WCF service. URL is: http://iphone.clickcelltest.com/EduLink.svc/GetLevel
Methods have following attribute:
[WebGet(ResponseFormat=WebMessageFormat.Json)]
I get Data from Database and then in Collection/List of classes.
I use Newtonsoft.Json DLL to convert the collection to json string. It does it very well but i need to send this to iPhone.
But, i get unnecessary escaping of quotes. I understand the reason why it is happening.
But, is there a way to avoid it
So, how can i change the current result:
"{\"Object\":[{\"LevelID\":4,\"LevelName\":\"Level A\"}]}"
To
{"Object":[{"LevelID":4,"LevelName":"Level A"}]}
Let me know if more clarity is required.
Thanks much in advance.

That looks like the result of returning a manually JSON-serialized string, which WCF is then serializing again. Rather than returning a string that you've built with Json.NET, make your service's return value match the type of data you're returning and return that data directly.
If you specifically need to use Json.NET for some reason, using an HttpHandler instead of WCF would allow you to respond at that lower level without WCF interfering.

Use stringByReplacingOccurencesOfString method in NSString class
NSString * jsonString = #"{\"Object\":[{\"LevelID\":4,\"LevelName\":\"Level A\"}]}";
NSString * filteredString = [jsonString stringByReplacingOccurrencesOfString:#"\\" withString:#""];
NSLog (#"%#",filteredString);

Related

Adding Instance Variable to NSData

So, I understand that Categories in Objective-C can be used to add methods to classes without the need for subclassing. I also understand that these Categories cannot be used to add instance variables to classes.
I did a little bit of reading about Class Extensions, which can be used to add instance variables, but I don't understand how I can use Class Extensions to modify an existing class such as NSData.
My problem is the following:
I have a Core Data Model that contains a NSURL and NSData. The NSData displays the data for the NSURL. When a view needs to display the data, I do the following check:
--- If [NSData bytes] > 0, display the NSData.
--- Otherwise, fetch the data at NSURL and display the data when it returns
Simple enough. However, I run into problems when the NSURL is updated. So, if I modify the NSURL path with a new image, because [NSData bytes] is already greater than 0, I don't make the additional call to fetch the new image.
What I would like to do is add an instance variable to NSData called URLKey that would hold information about where the data comes from. I can't subclass NSData because I'm using CoreData.
Does anyone know some simple solutions for this? Perhaps there's a gap in my understanding of Class Extensions, or maybe there's just no simple way.
Class Extensions should be used on classes you implement yourself as a way of keeping ivars and some properties hidden from the header File, that should contain only stuff that should be visible outside the class (and ivars are't that kind of stuff).
Categories are used on classes already implemented, as a way of adding additional functionality. They are usually needed when you want to add a general kind of behavior to a known Class. E.g. adding a method to NSString +(NSString*)reversedString; that returns a reversed instance so you can then use it like this:
NSString *someString = #"string";
NSString *reverse = [someString reversedString];
NSLog(#"%#", someString); //this would output "gnirts"
.
Regarding your particular problem, I can assure you that your CoreDataModel does not contain NSURL or NSData. The supported types are primitives, strings, binary Data and transformables. So, if you want to, you can subclass NSData or NSURL and then use it with CoreData by setting the type to "transformable". And after you have done this, you can then subclass NSData as you wish and use class extensions in the process, or just use a category to add the methods you require to the class.
Quote from Apple about transformable attributes:
The idea behind transformable attributes is that you access an
attribute as a non-standard type, but behind the scenes Core Data uses
an instance of NSValueTransformer to convert the attribute to and from
an instance of NSData. Core Data then stores the data instance to the
persistent store.

Iphone JSON-Framework

is posible using JSON-Framework for Iphone to know if a tag exists inside the JSON like in JAVA with the function hasTag(String)?
If by tag you mean name/key and you’re using SBJSON, use -objectForKey: and test if the return value is nil. For instance, if person is an NSDictionary instance returned by the JSON parser and it can optionally contain a nickname,
if ([person objectForKey:#"nickname"] != nil)
{
// `nickname' is available; do something with it
}
The way I think to do it is to convert your JSON Object to an NSDictionary and after that use the method -(NSArray *)allKeys, or -(NSArray *)allValues depending what you want.
It returns an array with all the keys (or values) inside the object. You can then compare the keys with the one you want to find.
Hope it helps you.

Dealing with <'null>' values in TouchJSON

I am deserializing some JSON using the TouchJSON framework. The array of dictionaries that comes from the parsing is used to populate a UITableView.
There is a chance that some of the values in the JSON I parse are empty. This results, if I NSLog it to the console, in the values looking like this:
id = 1234;
title = "Hello, World";
description = "<null>";
detail = "The world says hello";
Here the description value was an empty string when retrieved from the server.
So TouchJSON recognizes that the description values is of type string, but the original intention of the server was to communicate that this was an empty string, like description = #"";
If I later on try to set the value of description, to a UILabels text property the app will crash.
So my questions are, I have both NSNumbers and NSStrings in the JSON, should I traverse the result from TouchJSON's deserialize method and test all values and how would I do so?
I can't simulated what would happen if an NSNumber value was empty, how would I test for this? Will the NSNumber value be nil in that case instead of "null"?
I was using the SBJSON library and came up against the same problem. My solution would apply to your case too: I changed the library so that it handled missing values, setting them to +[NSNull null] in the collection it returned. That makes your client code a little warty, because you have to handle the cases where you might get an NSNull instead of an NSString. But this is just a more obvious version of the wart where you have to decide whether #"" meant an empty string or an unset value.

Is there a way to construct an object automatically from an NSDictionary?

I'm building an iPhone app that communicates with an external server via JSON. The JSON library I'm using parses the response string from the server into a dictionary. Currently I've got a method that I've written that just uses hardcoded strings as keys for the dictionary in a constructor I've written called initWithDictionary:(NSDictionary *)dic (e.g. self.name = [dic valueForKey:#"name"];. Is there some smart idiomatic Objective-C way to do this?
Just call [someObject setValuesForKeysWithDictionary:dictFromJSON] to set an object's properties from a dictionary.

Reflection in Objective-C (iPhone)

I want to populate [MyClass class] from a JSON string.
I use json-framework to get the NSDictionary, and it's dead easy to instantiate and setValue: forKey:... on my data object.
But for more complex data objects with classes as members of MyClass,
ie:
MyOtherClass *classNoTwo
I tried with
Class test = object_getClass(myClass.classNoTwo);
id foo = [[test alloc] init];
But foo is nil/null. The only way I found to get around it is to in my init method of MyClass is to alloc memory for it, and later replace it.
I would also like to know how to get rid of the myClass.classNoTo reference, as I am not supposed to know all the properties in my general parser.
Please don't just tell me to read the documentation, because I've done that, and I can't figure it out.
Thanks in advance
Try calling class_getProperty() to access a property of a particular name and then property_getAttributes() on the property returned by the first function. This will return a string that encodes the property's attributes, including the type. The format of the string is explained in Property Type Strings in the documentation.
Finally, when you have derived the type of the property, use NSClassFromString() to instantiate an object.
Also see the docs for the two functions mentioned for more details.
I have written a very simple dependency injection container called Factory. I do not get your question entirely, but the problems we solve look similar. Take a look at my sources, they are very simple and should get you started. You might be interested especially in the ClassAnalyzer class. It’s just a hack, but you should be able to get what you want from there.