1. First of all, I want to return multiple values in a method: return
the results of multiple posts in a method, and then I thought of putting the results of each request (Json data similar to the following example) in the Inside a Map, then serialize the Map again, and then return the serialized result.
Then it resulted, the returned result is this: (escaped (ps: this is not the kind of result I want))
So there are the following topics:
ps: how to build a Java project and run it (mainly for testing, my actual project is a javaWeb project)
2. Solve the problem of escaping the return value
. There is an article about how C# uses a dictionary to splicing and deserializing the data of Json data (with an array).
This is implemented in Java.
It is still spliced into such data, and then printed and
found to be escaped; find the reason
and finally found that the map data toString
will not be escaped after the toString will not be escaped. Print
these two maps and map.toString() I came out and found that the information is the same (isn't it amazing)
I first printed the Map object directly and the map.toString print result is the same, I don't understand why this happens
问:
java里面直接打印对象是可以出现对象的字符串的值,但是C#却是对象地址
答:因为Java里面的HashMap重写了toString方法
见下图:
问:
那map1只有在map1.toString()之后才会拼接成key=value格式字符串才对,为啥打印map1也是出来key=value格式字符串
答:
toString这个方法是自动执行的
也就是你有没有调toString他都会自己调
问:
我自己在这个类中重写一个toString()方法,为啥map.toString没有走?
答:
map实际上是HashMap的对象,他走的是HashMap的toString,不会走你这个类重写的toString
The object of the toString method of the parent class Object is the return object address, which is the same as C#
ps: Sometimes solving a problem is like solving a case, and a lot of theoretical knowledge has to be thought more by yourself. The
splicing demo is as follows:
package com.cn;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JsonToStr {
public static void main(String[] args)
{
JsonToStr jt=new JsonToStr();
String result=jt.dataToJson();
}
public String dataToJson()
{
Map<String,String> ma1=new HashMap<String,String>();
ma1.put("name", "Google");
ma1.put("url", "http://www.google.com");
List<Map<String,String>> ma2=new ArrayList<>();
ma2.add(ma1);
JSONArray maMap=JSONArray.fromObject(ma2);//.fromObject(ma2);
String links=maMap.toString();//数组序列化
Map<String,String> ma=new HashMap<String,String>();
ma.put("name", "BeJson");
ma.put("url", "http://www.bejson.com");
ma.put("links", links);
JSONObject maMap1=JSONObject.fromObject(ma);
String msStr=maMap1.toString();
Map<String,String> map1=new HashMap<String,String>();
map1.put("jsonStr", msStr);
map1.put("id","\"zh2789\"");
System.out.println("map1:"+map1);
JSONObject map1Map=JSONObject.fromObject(map1);
String result=map1Map.toString();
System.out.println("没有将map1使用toString方法返回的结果为:"+result);
System.out.println();
System.out.println("map1.toString():"+map1.toString());
JSONObject maPMap=JSONObject.fromObject(map1.toString());
String result2=maPMap.toString();
System.out.println("先将对象toString,然后再序列化:"+result2);
return result;
}
}
3. ps: Create a Java project in MyEclipse
1. Create a new Java project
2. Give the project a name (this process is the same as clicking finish directly)
3. Finish the project
4. Find our new project, and then add the Java class
5. Select src, right-click,
and then you can write the method.
Fourth, ps: and import the external jar package, because my serialized jar package depends too much, it is not recommended. The main steps are
to open the newly created project directory locally, and find that there is no lib directory.
The web project has a WebContent directory , the lib under WebContent is the jar package, but the Java project does
not
have
lib Paste the external jar package under the lib (ps is operated under the D drive,
not directly in MyEclipse)
4. After the addition is complete, select the project and select Build Path -> Configure Build Path
Five: ps runs the Java project, and the web project needs to start tomcat.
1. Select the java project and right-click->run as->java application (the class must have a main method)
2. After running, it is found that the jar package is missing , cycle four jar package addition steps
6. PS: Serialization of List Arrays
My jar package already has a method to serialize arrays, so I can use it directly, but it is recommended that Ali's FastJson.jar is easier to use