C# Json生成与解析

C# Json生成与解析

一、使用JsonData生成

生成如下Json形式(示例):

{
	"authenticateBasic": {
		"userID": "13512345678",
		"userType": "0",
		"authType": "4",
		"clientPasswd": "123456"
	},
	"authenticateTolerant": {
		"subnetID": "8601"
	},
	"authenticateDevice": {
		"deviceModel": "VR_HMT",
		"terminalVendor": "Pico",
		"physicalDeviceID": "11111111111111111111",
		"CADeviceInfos": [{
			"CADeviceType": "4"
		}]
	}
}

JsonData生成方法:

JsonData fields = new JsonData();

JsonData authenticateBasic = new JsonData();
authenticateBasic["userID"] = "13512345678";
authenticateBasic["userType"] = "0";
authenticateBasic["authType"] = "4";
authenticateBasic["clientPasswd"] = "123456";

JsonData authenticateDevice = new JsonData();
authenticateDevice["physicalDeviceID"] = "11111111111111111111";
authenticateDevice["deviceModel"] = "VR_HMT";
authenticateDevice["terminalVendor"] = "Pico";

JsonData CADeviceInfos = new JsonData();
JsonData CADeviceType = new JsonData();
CADeviceType["CADeviceType"] = "4";
CADeviceInfos.Add(CADeviceType);
authenticateDevice["CADeviceInfos"] = CADeviceInfos;

JsonData authenticateTolerant = new JsonData();
authenticateTolerant["subnetID"] = "8601";

fields["authenticateBasic"] = authenticateBasic;
fields["authenticateTolerant"] = authenticateTolerant;
fields["authenticateDevice"] = authenticateDevice;

Debug.LogError("YC ——————>  显示 JSON: " + fields.ToJson());

二、Json解析

解析如下Json形式(示例):

{
    "bandWidth": "-1",
    "antiTamperURI": [
        {
            "values": [
                "VODID,mediaID;result.retCode,authorizeResult.productID,playURL"
            ],
            "key": "PlayVOD"
        },
        {
            "values": [
                "productID;result.retCode"
            ],
            "key": "SubscribeProduct"
        },
        {
            "values": [
                "channelID,mediaID,businessType;result.retCode,authorizeResult.productID,playURL"
            ],
            "key": "PlayChannel"
        },
        {
            "values": [
                "categoryID,count;total,channelDetails.id,physicalChannels.btvCR.isSubscribed,physicalChannels.pltvCR.isSubscribed,physicalChannels.cutvCR.isSubscribed,physicalChannels.cpvrCR.isSubscribed,physicalChannels.npvrCR.isSubscribed,physicalChannels.cpltvCR.isSubscribed,physicalChannels.irCR.isSubscribed,physicalChannels.cpvrRecCR.isSubscribed,physicalChannels.npvrRecCR.isSubscribed"
            ],
            "key": "QueryChannelListByCategory"
        }
    ],
    "subscriberID": "13588480245",
    "userVODListFilter": "1066084244",
    "userVODDetailFilter": "1066084244",
    "profileID": "13588480245",
    "channelNamespace": "0",
    "userGroup": "-1",
    "cSessionId": "B4CFF8C1302F2A3AFC60C5C82E73688E",
    "bizDomains": [
        "2"
    ],
    "isFirstLogin": "0",
    "profiles": [
        {
            "isNeedSubscribePIN": "1",
            "channelListType": "1",
            "isDisplayInfoBar": "0",
            "isShowMessage": "0",
            "subscriberID": "13588480245",
            "isOnline": "0",
            "extensionFields": [
                {
                    "values": [
                        "0"
                    ],
                    "key": "refreshPasswordFlag"
                }
            ],
            "profileType": "0",
            "purchaseEnable": "1",
            "ratingName": "NC-17",
            "loginName": "13588480245",
            "ratingID": "4",
            "quota": "-1",
            "ID": "13588480245",
            "lang": "zh",
            "isDefaultProfile": "0",
            "hasCollectUserPreference": "0",
            "isSendSMSForReminder": "0",
            "profilePINEnable": "1",
            "isReceiveSMS": "0",
            "pushStatus": "1",
            "leadTimeForSendReminder": "5",
            "isFilterLevel": "0",
            "multiscreenEnable": "0",
            "createTime": "0",
            "name": "13588480245",
            "reminderInterval": "3"
        }
    ],
    "timeZone": "GMT+08:00",
    "loginOccasion": "0",
    "cpIds": [
        "-1"
    ],
    "opt": "0",
    "areaCode": "2",
    "loginIP": [
        "112.17.108.34"
    ],
    "userFilter": "1862116427",
    "isTriplePlay": "0",
    "bossID": "ZHEJIANGMOBILE",
    "needSignPrivacyStatement": "0",
    "registerNo": "81421579",
    "result": {
        "retMsg": "Successfully",
        "retCode": "000000000"
    },
    "isDeviceFirstLogin": "-1",
    "userPlaybillListFilter": "1130347676",
    "subnetID": "8601",
    "hasCollectUserPreference": "0",
    "RRSAddr": "http://112.17.52.41",
    "deviceID": "10001250211076",
    "userToken": "A8IhVA8IhV0XsoNvi25KxObGYc9ZR7HP",
    "jSessionID": "02H5HKH129RGF26OUW1D8L7P2YXL1MH7",
    "templateName": "default",
    "userPlaybillDetailFilter": "1130347676",
    "profileSN": "10000178496063",
    "userLoginHistoryInfo": {
        "curValidLoginItem": [
            {
                "profileSN": "10000178496063",
                "clientIP": "112.17.108.34",
                "subscriberSN": "10000178497279",
                "logindate": "1601343647000"
            }
        ],
        "latestSuccessItem": {
            "profileSN": "10000178496063",
            "clientIP": "112.17.108.34",
            "subscriberSN": "10000178497279",
            "logindate": "1601344167000"
        }
    }
}

解析方式:

[Serializable]
public class AuthentiateRespVoPico
{
        public string bandWidth;
        public object[] antiTamperURI;
        public string subscriberID;
        public string userVODListFilter;
        public string userVODDetailFilter;
        public string profileID;
        public string channelNamespace;
        public string userGroup;
        public string cSessionId;
        public string[] bizDomains;
        public string isFirstLogin;
        public AuthentiateDevicesRespVoPico[] devices;//设备
        public AuthentiateProfilesRespVoPico[] profiles;
        public string timeZone;
        public string loginOccasion;
        public string[] cpIds;
        public string opt;
        public string areaCode;
        public string[] loginIP;
        public string userFilter;
        public string isTriplePlay;
        public string bossID;
        public string needSignPrivacyStatement;
        public string registerNo;
        public ResultRespVoPico result;
        public string isDeviceFirstLogin;
        public string userPlaybillListFilter;
        public string subnetID;
        public string hasCollectUserPreference;
        public string RRSAddr;
        public string deviceID;
        public string userToken;
        public string jSessionID;
        public string templateName;
        public string userPlaybillDetailFilter;
        public string profileSN;
        public AuthentiateUserLoginHistoryInfo userLoginHistoryInfo;
}

[Serializable]
    public class AuthentiateDevicesRespVoPico
    {
        public string deviceType;
        public string fps;
        public string terminalVendor;
        public string physicalDeviceID;
        public string definition;
        public string deviceModel;
        public string ID;
        public string status;
        public string videoCodec;
        public string onlineState;
    }

    [Serializable]
    public class AuthentiateProfilesRespVoPico
    {
        public string isNeedSubscribePIN;
        public string channelListType;
        public string isDisplayInfoBar;
        public string isShowMessage;
        public string subscriberID;
        public string isOnline;
        public object[] extensionFields;
        public string profileType;
        public string purchaseEnable;
        public string ratingName;
        public string ratingID;
        public string loginName;
        public string quota;
        public string ID;
        public string lang;
        public string isDefaultProfile;
        public string profilePINEnable;
        public string hasCollectUserPreference;
        public string isSendSMSForReminder;
        public string isReceiveSMS;
        public string pushStatus;
        public string leadTimeForSendReminder;
        public string isFilterLevel;
        public string multiscreenEnable;
        public string createTime;
        public string name;
        public string reminderInterval;
    }
    
    [Serializable]
    public class AuthentiateUserLoginHistoryInfo
    {
        public AuthentiateValidLoginItem[] curValidLoginItem;
        public AuthentiateValidLoginItem latestSuccessItem;
    }
    
    [Serializable]
    public class ResultRespVoPico
    {
        public string retMsg;
        public int retCode;
    }


    public class APIHelper
    {
        public void AJson()
        {
          //response.text为返回文本
          JsonData jd = JsonMapper.ToObject(response.text);
          AuthentiateRespVoPico data = response.Deserialize<AuthentiateRespVoPico>();
          Debug.LogError("显示 Json:" + JsonConvert.SerializeObject(data));
        }
    }

你可能感兴趣的:(C# Json生成与解析)