How to parse JSON from String?

How to parse JSON in iOS 5?

  • I used iOS5 integrated twitter framework to request time line JSON, but I got wrong format JSON packet. For example use ( ) instead of [ ] for array use "<null>",not null. This result all JSON framework return an nil NSDictionary.

  • Answer:

    Can you show your code? I have trouble understanding your workflow here. "JSON" comes as a byte string (NSData). When you parse JSON ([NSJSONSerialization JSONObjectWithData:options:error:]), you convert that into native Objective-C data structures, like NSDictionary, NSArray, NSNumber, NSNull, representing the data in the JSON. Conversely, when you encode JSON ([NSJSONSerialization dataWithJSONObject:options:error:]), you take these Objective-C data structures and convert it into a JSON string again. When you say you see "()" and "<null>", those look like what you get when you print (i.e. call the -description on, or format with %@) NSArray and NSNull, and have nothing to do with JSON. It looks like you parsed JSON into native data structures, and then printed it out. So what you are seeing is normal. I don't know what you are trying to do. If you want to print the original JSON string, you should just convert the original JSON NSData into an NSString and print it. If you have Objective-C data structures you want to make into JSON, you need to encode it.

Xuan Luo at Quora Visit the source

Was this solution helpful to you?

Other answers

As the product lead in charge of developers at Magnet, I invite you to check out a new open source product we are about to release: rest2mobile. Rest2mobile generates native code for iOS, Android and JavaScript to access REST APIs. No need to build complex requests, parse JSON payloads, open remote connections, etc. The generated code supports sync and async and is type-safe. At the end, you just work with local objects or PoJo (Android) in your mobile app. It comes as a command line tool or as a plugin for Xcode, Android Studio and IntelliJ IDEA. More information is available here: https://developer.magnet.com/ You can also quickly check it out by following these 3 steps (OS X): Install with brew: brew install https://raw.githubusercontent.com/magnetsystems/r2m-cli/master/brew/r2m.rb Open a terminal and type: r2m Generate code for one of the pre-build samples: gen -l For example, from the GoogleDistance API, it generates the following method: // // Distance.h // // File generated by Magnet rest2mobile 1.0 - Oct 15, 2014 3:31:19 PM // @See Also: Magnet rest2mobile // #import <Rest2Mobile/Rest2Mobile.h> @class GoogleDistanceResult; @protocol DistanceProtocol <NSObject> @optional /** Generated from URL Page on googleapis.com GET maps/api/distancematrix/json @param origins style:QUERY @param destinations style:QUERY @param sensor style:QUERY @param mode style:QUERY @param language style:QUERY @param units style:QUERY @param success The block to be executed on the completion of a successful request. This block has no return value and takes one argument: the object/primitive constructed from the response data of the request. @param failure The block to be executed on the completion of an unsuccessful request. This block has no return value and takes one argument: the error that occurred during the request. @return A 'MMCall' object. */ - (MMCall *)googleDistance:(NSString *)origins destinations:(NSString *)destinations sensor:(NSString *)sensor mode:(NSString *)mode language:(NSString *)language units:(NSString *)units success:(void (^)(GoogleDistanceResult *response))success failure:(void (^)(NSError *error))failure; @end @interface Distance : MMController<DistanceProtocol> @end

Pascal Jaillon

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.