Hi @jeroen van den berg , Welcome to Microsoft Q&A,
// Decode NRBF data
using var stream = new MemoryStream(payload);
var rootRecord = NrbfDecoder.DecodeClassRecord(stream);
// Locate dictionary record
ClassRecord dictionaryRecord = rootRecord.GetClassRecord("MyDictionary");
// Extract key array
SZArrayRecord<string> keyArrayRecord = (SZArrayRecord<string>)dictionaryRecord.GetArrayRecord("Keys");
string[] keys = keyArrayRecord.GetArray();
// Extract value array
SZArrayRecord<int> valueArrayRecord = (SZArrayRecord<int>)dictionaryRecord.GetArrayRecord("Values");
int[] values = valueArrayRecord.GetArray();
// Rebuild dictionary
var dictionary = new Dictionary<string, int>();
for (int i = 0; i < keys.Length; i++) { dictionary[keys[i]] = values[i]; } // Output dictionary content foreach (var kvp in dictionary) { Console.WriteLine($"{kvp.Key}: {kvp.Value}"); }
updated:
The solution is working with a Dictionary<int, int> rather than a Dictionary<string, int>):
ArrayRecord keyValuePairsRecord = dictionaryRecord.GetArrayRecord("KeyValuePairs");
Array array = keyValuePairsRecord.GetArray(typeof(KeyValuePair<int, int>[]));
foreach (ClassRecord keyValuePairRecord in array.Cast<ClassRecord>())
{
int key = keyValuePairRecord.GetInt32("key");
int value = keyValuePairRecord.GetInt32("value");
Console.WriteLine(keyValuePairRecord);
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.