[IOS in the eyes of Android] IOS develops fast parsing of Json data (imitating Android parsing process)

Keywords: JSON Attribute Android github

Introduction (skip to the back if you want to see the method directly)

In the process of development, it is almost inevitable to deal with all kinds of data. Json, the structure carrier of data, is widely used because of its lightweight and simplicity.

For example, we need to parse the following Json data:

{
    "respCode": "0",
    "respMsg": "Get success",
    "data": 
        {
            "id": "999999",
            "name": "Zhang San",
            "age": 23
        }
 }

Android version

  • Here's a brief introduction to one of my favorite ways to implement json parsing in Android development (take Gson library parsing as an example)

    • Json Data Conversion into Class Structure
    • Network Acquisition of Corresponding Data
    • Resolve to Class instances using Gson

    Let's first convert the above data into java files

public class Model {
    String respCode;
    String respMsg;
    Data data;
    class Data {
        String id;
        String name;
        int age;
    }
}

//The above Json strings are then parsed using the Gson Library
Gson gson = new Gson();
Model model=  gson.formJson("For example Json Character string",Model.class);
//Then you can use the model instance to do anything.

Of course, the handwritten class structure is out. Here we recommend two tools to generate corresponding classes.
One is jsonschema2pojo
One is the plug-in GsonFormat in Android Studio
Of course, the method used is Baidu itself.

IOS version

I'm used to doing this in Android development. Recently, when I came into contact with IOS development, there are so many similarities in the implementation of the same model.

1.Json data is converted into struct or class in swift3

//Strct is a value type and class is a reference type. Strct is more efficient if the value needs little change.
//Here's an example of struct. Of course, the same thing happens when struct keywords are replaced by class es.
struct Model{
    var data: Data?
    var respCode: String?
    var respMsg: String?
}
struct Data{
    var id: String?
    var name: String?
    var age:Int?
}

Then you can use the Json parsing grammar that Swift3 comes with. Of course, that's more cumbersome. Let's not go into details here.
Handwritten struct is also very slow, here we recommend you to use a tool
https://github.com/zadr/j2s (free)
Download and import to xcode installation for use
There's no more introduction here, and with the source code, you can customize your own structure.

2.Json data transformed into struct (or class) instances
Here I recommend one to you.
Alibaba Open Source Reservoir https://github.com/alibaba/HandyJSON
Of course, I will not elaborate on the specific use of hair here.
Then struct becomes like this

struct Model: HandyJSON {
    var data: Date?
    var respCode: String?
    var respMsg: String?
}
struct Date: HandyJSON {
    var id: String?
    var name: String?
    var age:Int?
}

-----------Dividing line   
//If you get an example, you can do whatever you want.
let model = BaseType.deserialize(from: "For example Json Character string") 

Of course, there are more efficient ways to do this, and each of our architectures needs to be implemented: HandyJSON protocol is also very troublesome, we can use the j2s tools mentioned earlier, after downloading the source code, the transformation is as follows

 public var description: String {
        let typeName = name.generatedClassName()
        var d = ""
        properties.flatMap { (json) -> String? in
            if case .date(_, let format) = json.underlying { return format }
            return nil
        } .forEach {
            d += "\nprivate let \(typeName)DateFormatter\(abs($0.hashValue)): DateFormatter = {"
            d += "\n\tvar dateFormatter = DateFormatter()"
            d += "\n\tdateFormatter.dateFormat = \"\($0)\""
            d += "\n\treturn dateFormatter"
            d += "\n}()\n\n"
        }

        d += "struct \(typeName): HandyJSON {"
        d += "\(propertyDeclarationCode)\n"
        d += "\n"
        d += "}\n"
        return d
    }

If you don't want the corresponding extension, you can also study the next code to comment out the generation steps, and compile the project.
Finally, as shown in the figure.

Well, is it convenient?

Posted by gilsontech on Tue, 11 Dec 2018 05:33:06 -0800