preface

Using JSON format for communication between multiple ends, especially between the front and back ends, has become one of the mainstream schemes. PHP, Java, objectC and JavaScript are the main development languages that my team is in contact with.

Student: One question

Sometimes, in many cases, there will be data nesting phenomenon in the data provided by the interface. For example, in an accounting software, the data of a payment plan will carry the data of the contract in which the plan is located, and the contract data will carry the data of the contract corresponding to the customer.

We can of course take out the final value, the traditional way of taking a layer by layer.

However, there is a problem. Many times, this data does not always exist. For example, some contract data does not exist for the signing client, and if you do not null the code, the application may crash.

This is only about data extraction with multiple nesting, if your project has a convention to forbid data nesting, that’s another matter.

A common solution is needed

This problem exists in all development languages, so our team decided to make a common solution to solve it after discussion.

Stupid way

The core idea is very simple. We put forward the concept of path. For example, in the case of taking the contract customer name from the payment plan mentioned above, if path is used to locate the contract customer, > is used as the separator. The location path for the customer name is Results >pactUUIDLocal>customerUUIDLocal>name.

So, we just need to encapsulate a general method based on the path value, value, just call the method to fetch the value of each path, if there is no value return null, this problem is not solved.

Yes, that is the core idea of this article.

What about arrays

Think of the array as a dictionary with key as a subscript.

HaoResult .php .m .java .js

After constant tweaks, we define a HaoResult class that instantiates json objects and implements the core method haoresult.find (Path).

The code of each language implementation is as follows for your reference, respectively from HaoConnect and HaoAdmin:

HaoResult.php

HaoResult.m

HaoResult.java

haoresult.js

In fact, there are similar processing schemes in all languages. The most important significance of this paper is to propose and realize the data processing method of logical unification at each end.

It could do better

Why should we contract the path to a string instead of a string of arguments or an array? Because there’s an even better upgrade: search data by fuzzy path.

For example, if an interface returns a list of likes for an article, haoresult. find(“results>0>username”) can be used to locate the path to extract the data if you want the first username.

So what if you want to pull out all the user names?

Haoresult. search(“results>\d+>username”) to get an array of user names

In simple terms, the search method takes a string of regees, which are used to try to match all paths, and then retrieve all data that matches the paths.

After the language

I would have included more code in this article as a supplement, but when I think about it, the most important thing in this article is a direction to solve the problem, so why stack more code text?

The article provides the implementation code examples of each language, only for reference, can also be used after a little modification.

This article is a precursor to the Haoobject. js series, but stay tuned for details on how to play with data in Vue as well.

From the original star blog: wanyaxing.com/blog/201808…