当前位置: 首页>数据库>正文

unity 对接json数据 数组 unity post json

      Unity 3D与Android Studio通过Socket传输消息时,需要对Json数据进行处理。当有枚举数据时,对象转Json需要枚举转字符串,解析Json需要字符串转枚举。

一、Unity3D

public enum NetApiID
 
   
     { 
 
   
          NetApiID_Invaild = 0, 
 
   
          NetApiID_Login, 
 
   
          NetApiID_Pos,
 
   
          NetApiID_Chair, 
 
   
     }
 
   
     public class NetLogin
 
   
     {
 
   
          public NetApiID netApiID;
 
   
          public string name;
 
   
          public string id;
 
   
          public string phone;
 
   
     }

二、Android Studio

public enum NetApiID {
 
   
          NetApiID_Invaild, NetApiID_Login, NetApiID_Pos, NetApiID_Chair,
 
   
     }
 
   
 
   
     public class NetLogin {
 
   
          public NetApiID netApiID;
 
   
           
      
    public String name;
 
   
           
      
    public String id;
 
   
           
      
    public String phone;
 
   
     }

  • Unity3D对象转Json:

public void ObjToJson()
 
   
     {
 
   
           
      
    NetLogin msg = new NetLogin ();
 
   
           
      
    msg.netApiID=NetApiID.NetApiID_Login;
 
   
          msg.name="fcj";
 
              
   msg.id="142430199606222026";
 
   
           
      
    msg.phone = "12312312312";
 
   
           
      
    string str = JsonUtility.ToJson (msg);
 
   
     }

     [str结果]: {"netApiID":1,"name":"fcj","id":"142430199606222026","phone":"12312312312"}

  • Android Studio解析Json文件:

private void JsonToObj(String JsonData) {
 
    
           
       
     NetLogin netLogin = new NetLogin();
 
    
           
       
     try {
 
    
           
            
     JSONObject jsonObject = new JSONObject(JsonData);
 
    
           
            
     netLogin.MsgID=NetApiID.valueOf(jsonObject.getString("MsgID")); 
     //Java字符串转枚举
 
    
           
            
     netLogin.name = jsonObject.getString("name");
 
    
           
            
     netLogin.id = jsonObject.getString("id");
 
    
           
            
     netLogin.phone = jsonObject.getString("phone");
 
    
          } catch (Exception e) {
 
    
           
            
     Log.d(TAG, e.toString());
 
    
          }
 
    
     }

  • Android Studio对象转Json:

NetLogin netLogin=new NetLogin();
 
    
       
     netLogin.netApiID=NetApiID.NetApiID_Login;
 
    
       
     netLogin.name="fcj";
 
    
       
     netLogin.id="142430199606222026";
 
    
       
     netLogin.phone = "12312312312";
 
    
       
     JSONObject jsonObject=new JSONObject();
 
    
     try {
 
    
           
       
     jsonObject.put("netApiID",netLogin.netApiID);
 
    
           
       
     jsonObject.put("name", netLogin.name);
 
    
           
       
     jsonObject.put("id", netLogin.id);
 
    
           
       
     jsonObject.put("phone", netLogin.phone);
 
    
           
       
     String str=jsonObject.toString();
 
    
     }catch (JSONException e)
 
    
     {
 
    
          Log.d(TAG,e.toString());
 
    
     }

  • Unity 3D解析Json文件:

string msg=" 
     {'netApiID':1,'name':'fcj','id':'142430199606222026','phone':'12312312312'}";
 
    
     try
 
    
     {
 
    
        JsonNode node = JSON.Parse(msg);
 
    
        NetLogin netLogin = new NetLogin();
 
    
        
     netLogin.netApiID=(NetApiID)Enum.Parse(typeof(NetApiID),node["netApiID"]);//C#字符串转枚举
 
    
        netLogin.name = node["name"];
 
    
        netLogin.phone = node["phone"];
 
    
        netLogin.id = node["id"];
 
    
        }
 
    
     catch (Exception e)
 
    
     {
 
    
        Debug.Log(e.ToString());
 
    
     }


https://www.xamrdz.com/database/6mu1923194.html

相关文章: