1

I Have a JSON with this 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 parse this information to mi C# Code

I do this for Strings tags and works ok

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

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

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

but i get an error (Message=Can not convert Array to byte array. Source=Newtonsoft.Json).

edraven
  • 25
  • 2
  • 6
  • try to map this json object to a datacontract , you can use [link](http://json2csharp.com/) to make the class you can use as datacontract. this [link](http://msdn.microsoft.com/en-us/library/ms731073%28v=vs.110%29.aspx) may be helpful in doing that. – Zeshan Aman Feb 25 '14 at 08:00

4 Answers4

2

Try convert your alarms into a JArray:

JArray value = null;
value = obj["alarms"] as JArray;
Kevin Brechbühl
  • 4,717
  • 3
  • 24
  • 47
0

You could use JavaScriptSerializer for this.
For example:

JavaScriptSerializer jss = new JavaScriptSerializer();
Dictionary<string, object> dict = jss.Deserialize<Dictionary<string, object>>(jsonString);
Vlad Dekhanov
  • 1,066
  • 1
  • 13
  • 27
0

Using Json.Net

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

The casting should be JArray

jjchiw
  • 4,375
  • 1
  • 29
  • 30
-2

Your json string has a reserved c# keyword: interface. Try changing this and see if it works.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Oscar
  • 13,594
  • 8
  • 47
  • 75