Parsing object types and array-type JSON strings under Objective-c and Java

Source: Internet
Author: User

First, objective-c how to achieve:

There are 2 plugins to use, one for Jsonkit and the other for Jastor, with a total of 6 files, 3. h header files and 3. m implementation files. How to import third-party tools that do not support arc in an ARC project see this article: iOS in an arc-enabled project, import plug-ins for third parties that do not support arc

Specific documents on the Internet are relatively easy to find, you can download.

NSObject-type JSON string conversion to an object

The general idea is to first convert the JSON string to nsdictionary, and then generate the corresponding object by using the initialization method of the Nsdictionary parameter.

The process of generating nsdictionary is done by the NSString Objectfromjsonstring method provided by Jsonkit.

The process of generating the corresponding object with Nsdictionary is jastor to complete, and there are several requirements for this process:

    1. Your object must inherit the Jastor object;
    2. The property name of your object must correspond to the property names of the JSON string;
    3. If your object contains a list of custom objects, you need to write a separate class method for this property, with the rule "attribute name _class";

Examples are as follows:

The structure of the JSON string is as follows:

It contains information about a class, and 3 students in a class.

The corresponding data structure is as follows:

BMclass.h:

123456 @interface BMClass : Jastor@property(strong,nonatomicNSString* name;@property(strong,nonatomicNSString* grade;@property(strong,nonatomicNSArray* students;+(id)students_class;@end

BMCLASS.M:

1234567 @implementationBMClass@synthesize name,grade,students; +(id)students_class{    return[BMStudent class];}@end

BMStudent.h:

12345 @interface  < Code class= "OBJC plain" >bmstudent:jastor @property   (strong, nonatomic nsstring * name; @property   (strong, nonatomic )   nsstring * sex ; @property   ( Nonatomic int  age; @end

BMSTUDENT.M:

123 @implementationBMStudent@synthesizename,age,sex;@end

The specific parsing code is as follows:

12 NSDictionary* dic = [jsonStr objectFromJSONString];BMClass* c = [[BMClass alloc]initWithDictionary:dic];

The results of the parsed after operation are as follows (Debug):

  

Nsarray-type JSON string conversion to an object

If you get a string that is not of type "{}", but "[]" type, then the above parsing method does not apply, you need to

nsdictionary* dic = [Jsonstr objectfromjsonstring];

Revision changed to

nsarray* array = [Jsonstr objectfromjsonstring];

The obtained array is the Jkdictionary type, as follows;

To convert an object in an array to its own object, you need to iterate over the array, using the

bmclass* c = [[Bmclass alloc]initwithdictionary:dic];

Convert for each object.

================================= This is the split line =========================================

Now let's talk about how Java is implemented:

A Gson.jar package is required under Java.

The Java parsing JSON using Gson is slightly simpler than iOS, and the idea is to get the type of the object to be parsed and then parse it using the Fromjson method provided by Gson.

Examples of JSON strings that follow iOS above:

The Bmclass.class code is as follows:

12345 publicclass BMClass {    public String name;    public String grade;    publicList<BMStudent> students;}

The Bmstudent.class code is as follows:

12345 publicclass BMStudent {    public String name;    public String sex;    public intage;}
JSON string of type object converted to Objects

The parsing procedure code is as follows:

123 Gson gson = newGson();Type classType = newTypeToken<BMClass>() {}.getType();BMClass c = gson.fromJson(jsonStr, classType);

The results of the parsing are as follows (Debug):

JSON string of list type converted to Object

Only type types can be changed in the parsed place, for example:

1 Type classType = newTypeToken<List<BMClass>>() {}.getType();

Article ends.

Parsing object types and array-type JSON strings under Objective-c and Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.