1. Json recommendation
Do not use the JsonUtility that comes with unity
Its functions are various deficiencies and various problems
It is recommended to use Newtonsoft.Json powerful
The following is the dll file of Newtonsoft.Json, which can be used directly in the unity folder
Alibaba cloud disk sharing https://www.aliyundrive.com/s/Y7EYdHmeVbo
Simple use of Newtonsoft.Json in c#
JsonConvert.SerializeObject(object); //object to jsonJsonConvert . DeserializeObject < targetType > ( json ) ; //default json
[Note, conversion requirements]There is only one condition:
Fields converted to and from json must be public or wrapped by attributes
If you use C#'s Newtonsoft.Json to communicate with java
The json on the java side recommends using jackson
fastjson2 and Newtonsoft.Json are a bit json can not parse each other
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency>
Java's jackson is simple to use
ObjectMapper objectMapper = new ObjectMapper(); objectMapper.writeValueAsString(object); //【对象转json】ObjectMapper objectMapper = new ObjectMapper();
objectMapper.readValue(json, ClassType); //【json转对象】
have to be aware of is:
new ObjectMapper();
Every new jackson comes out is a brand new and different jackson
Therefore, it is recommended to use a new one and save it for public use. It is not recommended to use a new one every time.
2. Json and byte[] data conversion
Sometimes we need to convert json to bytes for transmission, the simple conversion is as follows
C#:
Encoding.UTF8.GetBytes(json); //【json转byte[]】Encoding.UTF8.GetString(bytes); //【byte[]转json】
java:
json.getBytes("utf-8"); //【json转byte[]】
new String(bytes, "utf-8"); //【byte转json】
3. Client server json communication data body
In order to avoid unnecessary errors, it is recommended that the communication data meet the following points
1. Private fields
2. Wrapping fields (jack remembers to set properties to ignore size padding)
3. Empty parameter constructor
4. Do not put the extension method in the communication data
public class NetData
{
private HashMap<String,Object> mapDatas;
public NetData()
{
this.mapDatas = new HashMap<>();
}
public NetData add(String k,Object o){
mapDatas.put(k,o);
return this;
}
public <T> T get(String k,Class<T> vType){
T t = (T) mapDatas.get(k);
return t;
}
public HashMap<String, Object> getMapDatas()
{
return mapDatas;
}
public void setMapDatas(HashMap<String, Object> mapDatas)
{
this.mapDatas = mapDatas;
}
}
public class NetDataHelp
{
//int1 string2
public static NetData addSingleInt(int v){
return new NetData().add("1",v);
}
public static int getSingleInt(NetData netData){
return netData.get("1",int.class);
}
public static NetData addSingleString(String v){
return new NetData().add("2",v);
}
public static String getSingleString(NetData netData){
return new NetData().get("2",String.class);
}
}
4. Trample records transmitted by Json
1. Case sensitive
name="Xiao Ming" cannot be filled in the Name field
java jackson
If your field is private and wrapped by getset
Then when json is parsed, this field starts with lowercase by default.
For example, getName will be parsed as name (beginning with lowercase)
newtonsoft for C#
If your fields are private and wrapped by properties
Then when json is parsed, this field starts with uppercase by default.
For example, Name{get;set} will be parsed as Name (beginning with uppercase)
When jackson and C# interact with json, the private properties are different after each package
The java field is a lowercase name
C# fields are capitalized Name
causes the field to fail to fill
solution
Set the jackson object you use publicly as follows
objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES,true);This setting is to ignore the case of the attribute filling, even the Name can be filled into the name
2. Method naming convention
If you want to add auxiliary methods in the data transfer class in java, don’t name them getXXX and setXXX
When json fills the field, it will mistakenly think that this is a field wrapper
For example, write a get/setSingleString method in the transmission class
Then it’s over, when json is filled, he will automatically find the SingleString field
In short, I still hope that there are no methods in the data transmission class, only the data body, etc.
Helper methods can be written in another class.