JSON to Array C #

I have JSON with these values

{"interface":"ALL",
 "dv":"1",
 "alarms":[{"Id":0,
            "Type":"URL",
            "Trigger":"facebook",
            "Output":"video"},
           {"Id":1,
            "Type":"URL",
            "Trigger":"twitter",
            "Output":"video"},
           {"Id":2,
            "Type":"URL",
            "Trigger":"ebay",
            "Output":"video"}
]}

      

And I would like to parse this information into mi c # code

I am doing this for Strings tags and it works fine

JObject obj = JObject.Parse(json);
String value =(String) obj["dv"]; 

      

but I have an error for Array alarms tags. I've tried with:

Array value = null;
value =(Array) obj["alarm"]; 

      

but I am getting the error (Message = Cannot convert array to byte array. Source = Newtonsoft.Json).

+1


source to share


4 answers


Try converting alarms

to JArray :



JArray value = null;
value = obj["alarms"] as JArray;

      

+1


source


You can use JavaScriptSerializer

for this.
For example:



JavaScriptSerializer jss = new JavaScriptSerializer();
Dictionary<string, object> dict = jss.Deserialize<Dictionary<string, object>>(jsonString);

      

0


source


Using Json.Net

JArray value = null;
value = obj["alarms"] as JArray;

      

Casting must be JArray

0


source


In your line json have reserved keyword C #: interface

. Try changing this and see if it works.

-2


source







All Articles