Remember the helplessness of parsing json once and think of a better way to realize the public method sharing of json parsing

Recently, I received a relatively important project. Due to the need to connect with multiple third-party departments or platforms, it is inevitable to use various methods of transformation and big moves. Various docking methods have been moved out. Many json parsing parts and solutions are used in the project. The pain points of docking in heterogeneous environments, of course, the uneven standards and various formats provided by each department and docking platform make it very difficult for downstream docking to understand. . . It can only be hard-coded. Let’s take a look below. The brainless json returned by a docking department is as follows:

{"code":0,"message":"成功","result":"[{\"desc\":\"姓名\",\"name\":\"XM\",\"value\":\"徐猗x\"},{\"desc\":\"手机号码\",\"name\":\"SJH\",\"value\":\"13832162xxx\"},{\"desc\":\"身份证号\",\"name\":\"SFZ\",\"value\":\"11011219970730xxxx\"},{\"desc\":\"性别\",\"name\":\"XB\",\"value\":\"女\"},{\"desc\":\"借款区域\",\"name\":\"HZSQY\",\"value\":\"北京市北京市海淀\"},{\"desc\":\"单位电话\",\"name\":\"DWDH\",\"value\":\"1867876xxxx\"},{\"desc\":\"住宅电话\",\"name\":\"ZZDH\",\"value\":\"\"},{\"desc\":\"父母所在地\",\"name\":\"FMSZD\",\"value\":\"北京市北京市海淀-北京市海淀区\"},{\"desc\":\"婚姻状况\",\"name\":\"HYZK\",\"value\":\"未婚\"},{\"desc\":\"单位地址\",\"name\":\"DWDZ\",\"value\":\"\"},{\"desc\":\"现居住地\",\"name\":\"XJZD\",\"value\":\"北京市北京市海淀-北京市海淀区\"},{\"desc\":\"信修手机号\",\"name\":\"XXSJH\",\"value\":\"\"},{\"desc\":\"配偶姓名\",\"name\":\"POXM\",\"value\":\"\"},{\"desc\":\"配偶证件号\",\"name\":\"POSFZ\",\"value\":\"\"},{\"desc\":\"配偶手机\",\"name\":\"POSJH\",\"value\":\"\"},[[{\"desc\":\"姓名\",\"name\":\"LXRXM\",\"value\":\"紧急联系人1\"},{\"desc\":\"手机号\",\"name\":\"LXRSJH\",\"value\":\"18070542xxx\"},{\"desc\":\"与借款人关系\",\"name\":\"LXRGX\",\"value\":\"亲属\"}],[{\"desc\":\"姓名\",\"name\":\"LXRXM\",\"value\":\"紧急联系人2\"},{\"desc\":\"手机号\",\"name\":\"LXRSJH\",\"value\":\"13136614xxx\"},{\"desc\":\"与借款人关系\",\"name\":\"LXRGX\",\"value\":\"亲属\"}],[{\"desc\":\"姓名\",\"name\":\"LXRXM\",\"value\":\"紧急联系人3\"},{\"desc\":\"手机号\",\"name\":\"LXRSJH\",\"value\":\"13463141xxx\"},{\"desc\":\"与借款人关系\",\"name\":\"LXRGX\",\"value\":\"亲属\"}]],{\"desc\":\"村委联系人\",\"name\":\"CWLXR\",\"value\":\"\"},{\"desc\":\"村委联系人电话\",\"name\":\"CWLXRDH\",\"value\":\"\"},{\"desc\":\"行政村村名\",\"name\":\"CM\",\"value\":\"\"}]"}

First of all, let's not talk about the return problem for the time being. Since it is json interaction, the buddy first thinks that the format returned by the result with backslashes is provided by entry-level novices. Well, bear with it, continue to analyze, then take the result first Come out and have a look, the result is easier to handle and directly convert the String through json resultstr = (String) jsonObject.get("result"); just take it out, as follows:

[{"desc":"姓名","name":"XM","value":"徐猗x"},{"desc":"手机号码","name":"SJH","value":"13832162xxx"},{"desc":"身份证号","name":"SFZ","value":"110112199707303xxx"},{"desc":"性别","name":"XB","value":"女"},{"desc":"借款区域","name":"HZSQY","value":"北京市北京市海淀"},{"desc":"单位电话","name":"DWDH","value":"18678765xxx"},{"desc":"住宅电话","name":"ZZDH","value":""},{"desc":"父母所在地","name":"FMSZD","value":"北京市北京市海淀-北京市海淀区"},{"desc":"婚姻状况","name":"HYZK","value":"未婚"},{"desc":"单位地址","name":"DWDZ","value":""},{"desc":"现居住地","name":"XJZD","value":"北京市北京市海淀-北京市海淀区"},{"desc":"信修手机号","name":"XXSJH","value":""},{"desc":"配偶姓名","name":"POXM","value":""},{"desc":"配偶证件号","name":"POSFZ","value":""},{"desc":"配偶手机","name":"POSJH","value":""},[[{"desc":"姓名","name":"LXRXM","value":"紧急联系人1"},{"desc":"手机号","name":"LXRSJH","value":"18070542xxx"},{"desc":"与借款人关系","name":"LXRGX","value":"亲属"}],[{"desc":"姓名","name":"LXRXM","value":"紧急联系人2"},{"desc":"手机号","name":"LXRSJH","value":"1313661xxxx"},{"desc":"与借款人关系","name":"LXRGX","value":"亲属"}],[{"desc":"姓名","name":"LXRXM","value":"紧急联系人3"},{"desc":"手机号","name":"LXRSJH","value":"1346314xxxx"},{"desc":"与借款人关系","name":"LXRGX","value":"亲属"}]],{"desc":"村委联系人","name":"CWLXR","value":""},{"desc":"村委联系人电话","name":"CWLXRDH","value":""},{"desc":"行政村村名","name":"CM","value":""}]

I didn’t feel any problem at the beginning, okay, let’s continue to analyze, wait a minute, ri~ What’s the situation, see the picture below:

What the hell is this? It's okay to set objects in the array, but also set the array, doesn't it look awkward? Yes, people who see it at the first glance will be stunned for a long time, what is this thing, try to analyze it, continue to click on the array and find that it is a nonsensical array, fortunately, I have a strong mental capacity, the keys in it are all the same, I Be a good boy~ Continue to bear it. If there is a problem, you have to continue to pick it out. Although you don’t know what will be put in the array in the future, but every time you pick up the third layer, you know that it is A? B? C? guarantor? emergency contact? Co-borrower? It’s really impossible to guess, brother suddenly felt very high, like drinking two or two Erguotou, but the alcohol capacity is small~ Hehe, work has to continue, there are several pits waiting for us, everyone has dug them with good intentions, so let’s jump Come on, bring a bed sheet, you can roll the bed sheet when you roll to the bottom of the pit, woo hoo. . . . .

Regardless of whether it is low or not, let’s upload a piece of code first. After a long time of tossing, I finally figured it out, haha

How about it? Awesome or not, every time I don’t know whether the array is hiding a god or a ghost? So let's first identify whether it is a livestock or a livestock, and then analyze it, but after writing it, I always feel that something is wrong. Look at the analyzed MAP, let me go, let's use the name as the key. . . .

But why, exactly why, it’s fine if the array doesn’t have a defined name, and the object name given in it is also repeated. Well, don’t worry, let me calm down with a goose feather fan first. . After all, the two ounces just now started to rise again. Now that I know my drinking capacity, I will drink one or two next time~ Huhu, it’s okay, I can’t help me if I repeat it, let’s see how I get you, since it’s repeated, right? Well, let’s put you together repeatedly, although you refuse to share or group rent, there is no way~ Brothers, let’s squeeze and squeeze a field first, let’s splice the same name together first, since the key is the same , it doesn't matter, it will never be wrong to take it out according to the key,

JSONObject jsonObject = JSONObject.fromObject(result);
				String resultstr = (String) jsonObject.get("result");
				JSONArray jsarray =  JSONArray.fromObject(resultstr);
				Map<String, String> map = new HashMap<String, String>();
				for(int i=0;i<jsarray.size();i++){
					if( jsarray.get(i) instanceof JSONObject){
						map.put((String) ((JSONObject)jsarray.get(i)).get("name"), (String) ((JSONObject)jsarray.get(i)).get("value"));
					}else{
						JSONArray jsarray1 = (JSONArray) jsarray.get(i);
						String keyName="";
						for(int j=0;j<jsarray1.size();j++){
							JSONArray jsarray2 = (JSONArray) jsarray1.get(j);
							for(int n=0;n<jsarray2.size();n++){
								if( jsarray2.get(n) instanceof JSONObject){
									keyName = (String) ((JSONObject)jsarray2.get(n)).get("name");
									if(null!=map.get(keyName)){
										String keyValue = map.get(keyName)+","+ (String)((JSONObject)jsarray2.get(n)).get("value");
									    map.put(keyName, keyValue);
									}else{
										 map.put(keyName, (String) ((JSONObject)jsarray2.get(n)).get("value"));
									}
								}
							}
						}
					}
				}
				System.out.println("JSONObject==="+JSONObject.fromObject(map));

How about it, let’s talk about it, no matter how difficult the problem is, it’s easy to solve, let’s take it out and see what it looks like, haha

It looks much more comfortable, just map get (key) directly when using it, ou yeah! Done, come back and check the code, I always feel uncomfortable,

Okay, let’s optimize it again, we can’t keep going low, low is not our goal, it’s really unsightly to write like this, and it doesn’t fit my style of doing things, okay, stop the goose feather fan, and fan again I just have a cold, and I can’t go out casually when I have a cold. Take advantage of my calm mind, optimize it quickly, write a method to eliminate all the code, and simplify it

Now that so many codes have been eliminated, have they all been transferred to the red box method? It’s so funny. Did you transfer it? Of course not, haha

Continue to read, how is the little recursion written, no matter what type it is, take it all, eat a ghost, and there is a god? Don't care, keep eating until the turtle, see what you pretend, look in the demon mirror~

Is it okay to throw the array in every time, no matter what type is in it, put it in the map, yes, it is like this after a simple analysis, well, since I don’t know how many of your array will be returned to me, or The quantity is not fixed, or the type is not fixed, I will accept it for you, and the ones with the same name will be spliced ​​together for you.

	private Map<String, String> searchKey(Map<String, String> map,JSONArray jsarray) {
		String keyName="";
		for(int i=0;i<jsarray.size();i++){
			if( jsarray.get(i) instanceof JSONObject){
				keyName = (String) ((JSONObject)jsarray.get(i)).get("name");
				if(null!=map.get(keyName)){
					String keyValue = map.get(keyName)+","+ (String)((JSONObject)jsarray.get(i)).get("value");
					map.put(keyName, keyValue);
				}else{
					 map.put(keyName, (String) ((JSONObject)jsarray.get(i)).get("value"));
				}
			}else{
				map=searchKey(map,(JSONArray)jsarray.get(i));
			}
		}
		return map;
	}

Well, test it, ok ou yeah ~ this is the way ~ look at the return value, yes it is exactly the same as the return value just now, isn’t it simple? Well, let’s abstract the method and use it in other places, haha

Call it in a loop, how many arrays and how many objects can take it all, and output the map uniformly 

Guess you like

Origin blog.csdn.net/fuhaiqiang123/article/details/105750581