Andreas Reitberger
Andreas Reitberger

Reputation: 217

C# serialize JSON without property name

maybe this was asked somewhere before, but I don't know how to search for my problem. I'm using a WebAPI to verify licenses. From this API I get the return JSON string in follwing format.

string json = "[{"status":"error","status_code":"e002","message":"Invalid licence key"}]"

This is my serializer

using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
{
                DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(ActivationResponseWooSl));
                ActivationResponseWooSl ar = (ActivationResponseWooSl)js.ReadObject(ms);
}

Now my questions is, how must the "ActivationResponseWooSl" class look like so that the serializer can convert it?

Any help is highly appreciated!

For now it looks like that (what's not workiing):

[DataContract]
public class ActivationResponseWooSl
{
    [DataMember(Name = "status")]
    public string Status { get; set; }

    [DataMember(Name = "status_code")]
    public string ErrorCode { get; set; }

    [DataMember(Name = "message")]
    public string ErrorMessage { get; set; }
}

However when I serialize my json string, all properties are "null".

Upvotes: 0

Views: 684

Answers (1)

Antoine V
Antoine V

Reputation: 7204

Your class is correct. Your JSON is an array of object.

Try

DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(ActivationResponseWooSl[]));
var arr = (ActivationResponseWooSl[])js.ReadObject(ms);

Upvotes: 1

Related Questions