Search code examples
c#stringrtmp

Convert long string into multiple short strings


I have a very long code that a server creates. Here is the code that is used:

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

The client has to convert this string into a shorter string that only contains:

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

If statements won't work because these are randomized. I am not in power of the server so I can not change the method that it creates the string


Solution

  • You can convert that string on the jsonObject that represent it. And after that make your own string. Take the newtonsoft from nuget repository.

    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);