Convert a JavaScript Array to a c# class

时间:2016-10-20 20:04:09

标签: javascript c# arrays json

i've got a JavaScript-Arrays of following format

['abc','efg','hik','lmn','','',' / ','','',13512,13520,1,'',0,2488934,1,1004,1,'','','1.00']

And I want to convert them to a c#-class. I thought this would be possible with Newtonsoft JSON.NET

My first attempt was List<string> objs = JsonConvert.DeserializeObject<List<string>>(stringValue);

That was working. Now I created a classs

 class ArrayData
{ 
   [JsonIgnore]
    public long id{ get; set; }

    [JsonProperty(Order=1)]        
    public String a { get; set; }
    [JsonProperty(Order = 2)]
    public String b { get; set; }
    [JsonProperty(Order = 3)]
    public String c { get; set; }
    [JsonProperty(Order = 4)]
    public String d { get; set; }
    [JsonProperty(Order = 5)]
    public String e { get; set; }
    [JsonProperty(Order = 6)]
    public String f { get; set; }
    [JsonProperty(Order = 7)]
    public String g { get; set; }
    [JsonProperty(Order = 8)]
    public String h { get; set; }
    [JsonProperty(Order = 9)]
    public String i { get; set; }
    [JsonProperty(Order = 10)]
    public String j { get; set; }
    [JsonProperty(Order = 11)]
    public String k { get; set; }
    [JsonProperty(Order = 12)]
    public String l { get; set; }
    [JsonProperty(Order = 13)]
    public String m { get; set; }
    [JsonProperty(Order = 14)]
    public String n { get; set; }
    [JsonProperty(Order = 15)]
    public String o { get; set; } 
    [JsonProperty(Order = 16)]
    public String p { get; set; }
    [JsonProperty(Order = 17)]
    public String q { get; set; }
    [JsonProperty(Order = 18)]
    public String r { get; set; }
    [JsonProperty(Order = 19)]
    public String s { get; set; }
    [JsonProperty(Order = 20)]
    public String t { get; set; }
    [JsonProperty(Order = 21)]
    public String u { get; set; }
}

And tried following var myObj = JsonConvert.DeserializeObject<ArrayData>(stringValue);

But now I got the error: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ArrayData' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1.

Is there no way I can assign a class attribute to an anonymous field of an array just by position with JSON.NET?

0 个答案:

没有答案