简体   繁体   中英

C# 2 dimensional byte[][] to JSON.NET to C# byte[][] or Javascript equivalant fails

I need to send byte[][] from a C# WCF webservice to a JSON client.

The raw WCF result is the following (Look for AttributeData in the second to last line):

{"sa":["BCtVgTmz5ZcCH34SZcsQrWQq3B7QVXSiPE57Vj7yZ/TwDNO+V3UkMr6wZKn8Fiu9+/YNG0iESAKz4EcbimeLzQU=","BHez/TgkoKbRwTu/lqS18h1hagY9N9j6/+cZhuHacWPPTgsoMOzNDInKDR7j6M+1VAqRtCRVC2qFI4q6NFE0bRQ=","BL4DnZQMV/kLa5n+LviukZI2wSbsNmJb7R/Zs51Z0neRmC/JJkw5hmmU7Pkl9WoHjlMR+CERNehOiLaYvUqZfyU=","BPdEcJW8iVB74Cvxm720vVwxDnbVTm/jrkZ5vPb12JOXTltcTOt7wH9IeMQG9oArvgdjTevmvif09TVtaaT7uLs=","BFOZjjprurgN8pQszEkrUOvMybTFuj5Uitg563Rh08PoukbRcfGlyhLuYzev8m94h1fCv+Zg8i1PI0vWkgOEmlM="],"sb":["BNTHLQWFFN/gFhQ+XaWfuONLZHBXHc1UMTqYSRtEPaqeSMe99wz3pKnHQdICzsvepY6btIRPLVFZCzK5MQ7v5Do=","BL6NBGy6vzJmPOJKu+WMQffLvnYw8gA+7ZYiANTOP+/5YpVELhsUva9OxS6CugsYvsaJIiAkV96ZbBVAyp9/2D0=","BEWrUO7QXBPH68LEk7QTXAf064fBe8KTpy8MqsfcAAZ/nB10IHMnKBSdiU+nwNhnSnbc7zCsDSzA825iDQLv9yU=","BIJYfLEMeF+V6IG9jZBcEP4vp0FhEejX46uEup/lY890XNQEljFy4V1NUPSgDfTGxketJLyuaf+0lTY1aeDEGTI=","BMwBbG/n3ylmxyo3wbxe6nODXyvJ0VpldnRZ6Wgpkn7CVcJMyXTMmhv5rxwwshq5Wbt6tFA4c3Bwy284KAAj5yI="],"sz":"BN39ATET17jdkI02lCEr5eQQpRS2n3f1Q2tE+BTDNY/HuLCCo8Gu3hHHgPVX7/kPN7KmH BJlmqniKBxx5nOcXwE=",

"sid":"this is the new session id","ti":"TokenData","NumberOfTokens":5,"AttributeData":["QXR0cmlidXRlMA==","QXR0cmlidXRlMQ==","QXR0cmlidXRlMg==","QXR0cmlidXRlMw==","QXR0cmlidXRlNA=="]}

Client

The code for the C# consumer looks like this:

 var converter = new ExpandoObjectConverter();
 dynamic obj = JsonConvert.DeserializeObject<ExpandoObject>(dataJsonAsListedAbove, converter);
 byte[][] a = (byte[][]) obj.AttributeData;
//  byte[][] a = obj.AttributeData;  // same error

The error that occurs on the last line is below

RuntimeBinderException was unhandled

An unhandled exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in System.Core.dll

Additional information: Cannot implicitly convert type 'System.Collections.Generic.List' to 'byte[][]'

Question

How can I properly decode a two dimensional Array[][] from JSON in C#?

For your reference the C# server code looks something like this:

        byte[][] attributes = null;
        if (attributes == null)
        {
            // Evaluate the client proposed attributes and add demo data in certain circumstances
            List<byte[]> attList = new List<byte[]>();
            for (int i = 0; i < ikap.IssuerParameters.E.Length; i++)
            {
                attList.Add(encoding.GetBytes("Attribute" + i));
            }
            attributes = attList.ToArray();
        }

   //
  //  snip.. .firstMessage is a JSONString that we are appending to using this awkward method
  //
    dynamic obj = JsonConvert.DeserializeObject<ExpandoObject>(firstMessage, converter);
        obj.sid = "this is the new session id";
        obj.ti = "TokenData"; 
        obj.NumberOfTokens = 5;
        obj.AttributeData = attributes;

    // Returning the JSON as a stream so that .NET WCF doesn't escape the raw JSON we're sending.
    return returnAsStream(JsonConvert.SerializeObject(obj));

I don't see any 2-D array in your json...

var obj  = JsonConvert.DeserializeObject<AClass>(DATA);
foreach (var data in obj.AttributeData)
{
    var buf = Convert.FromBase64String(data);
    Console.WriteLine(Encoding.UTF8.GetString(buf));
}

public class AClass
{
    public List<string> sa { get; set; }
    public List<string> sb { get; set; }
    public string sz { get; set; }
    public string sid { get; set; }
    public string ti { get; set; }
    public int NumberOfTokens { get; set; }
    public List<string> AttributeData { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM