将长字符串转换为多个短字符串

时间:2014-06-07 06:53:18

标签: c# string rtmp

我有一个服务器创建的很长的代码。以下是使用的代码:

AllGameData = "{\"queueId\":" + queueId + ",\"isRanked\"."+ isRanked + ",\"rankedTeamName\":\"" + rankedTeamName + "\",\"mapId\":" + mapId + ",\"gameTypeConfigId\":" + gameTypeConfigId + ",\"gameMode\":\"" + gameMode + ",\"gameType\":\"" + gameType + "\"}";

客户端必须将此字符串转换为仅包含以下内容的较短字符串:

mapId: [int mapId]
gameMode: [string gameMode]
gameType: [string gameType]

如果语句无效,因为这些语句是随机的。 我不掌握服务器的功能所以我无法改变它创建字符串的方法

2 个答案:

答案 0 :(得分:0)

你可以把这个字符串转换成Json, 就像为必填字段创建一个类,你可以将字符串序列化为ur对象, 这样用户可以单独获取每个字符串

谢谢

答案 1 :(得分:0)

您可以在表示它的jsonObject上转换该字符串。然后制作自己的字符串。从nuget存储库获取newtonsoft。

using Newtonsoft.Json; // This is the namespace of Newtonsoft. 

// this are the lines
dynamic gameDataObj = JsonConvert.DeserializeObject(AllGameData);

var newStr = string.Format("{{\"mapId\":\"{0}\", \"gameMode\":\"{1}\", \"gameType\":\"{2}\"}}", gameDataObj.mapId, gameDataObj.gameMode, gameDataObj.gameType);
相关问题