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:
- Your object must inherit the Jastor object;
- The property name of your object must correspond to the property names of the JSON string;
- 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,
nonatomic
)
NSString
* name;
@property
(strong,
nonatomic
)
NSString
* grade;
@property
(strong,
nonatomic
)
NSArray
* students;
+(
id
)students_class;
@end
|
BMCLASS.M:
1234567 |
@implementation BMClass @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 |
@implementation BMStudent @synthesize name,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 |
public class BMClass { public String name; public String grade; public List<BMStudent> students; } |
The Bmstudent.class code is as follows:
12345 |
public class BMStudent { public String name; public String sex; public int age; } |
JSON string of type object converted to Objects
The parsing procedure code is as follows:
123 |
Gson gson = new Gson(); Type classType = new TypeToken<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 = new TypeToken<List<BMClass>>() {}.getType(); |
Article ends.
Parsing object types and array-type JSON strings under Objective-c and Java