2

I have a JSON

{{  "action": "rma",  "devices": "[95001105,30013103,300117]",  "devandreason": [    {      "device": 95001105,      "reason": 100    },    {      "device": 30013103,      "reason": 300    },    {      "device": 300117,      "reason": 200    }  ]}}

for which I'm trying to get the devandreason as an array. I've tried creating classes

public class DevReasonList
    {
        public List<DevReason> devandreason { get; set; }
    }
    public class DevReason
    {
        public Double device { get; set; }
        public Double reason { get; set; }
    }

and the json_serializer:

JavaScriptSerializer json_serializer = new JavaScriptSerializer();
DevReasonList deviceAndReasonList = json_serializer.Deserialize<DevReasonList>(json.devandreason);

but it throws an exception:

json_serializer.Deserialize<DevReasonList>(json.devandreason) 'json_serializer.Deserialize<DevReasonList>(json.devandreason)' threw an exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' dynamic {Microsoft.CSharp.RuntimeBinder.RuntimeBinderException}

But I don't know what I'm doing wrong :(

It is possible to deserialize devandreason and make it an array?

4
  • 1
    Check out json2csharp.com. It's quite helpful when deserializing json. Commented Apr 28, 2017 at 14:58
  • 1
    Your JSON is not valid. Commented Apr 28, 2017 at 14:58
  • Any specific reason you have quotes between [95001105,30013103,300117] ? Commented Apr 28, 2017 at 15:53
  • The devices list has integer values. That's what I'm receiving in the controller after calling Ajax with json.stringify Commented Apr 30, 2017 at 17:35

1 Answer 1

5

This should be your model according to

public class Devandreason
{
    public int device { get; set; }
    public int reason { get; set; }
}

public class RootObject
{
    public string action { get; set; }
    public string devices { get; set; }
    public List<Devandreason> devandreason { get; set; }
}

I removed the beginning { and trailing } and it now validates

{  "action": "rma",  "devices": "[95001105,30013103,300117]",  "devandreason": [    {      "device": 95001105,      "reason": 100    },    {      "device": 30013103,      "reason": 300    },    {      "device": 300117,      "reason": 200    }  ]}

Bonus: http://json2csharp.com/

Edit: As raised by @Sven in the comments: RootObject would be so much easier to traverse if your devices type was List.

Here is the json that would be required, I just removed the quotes before the value:

{  "action": "rma",  "devices": [95001105,30013103,300117],  "devandreason": [    {      "device": 95001105,      "reason": 100    },    {      "device": 30013103,      "reason": 300    },    {      "device": 300117,      "reason": 200    }  ]}
Sign up to request clarification or add additional context in comments.

5 Comments

sweet sweet link.
Why is "devices" not a List<int>?
Nvm I just noticed the quotes around the []
@Sven, you raise a good point however, If the Json could be changed to remove the quotes, then this model would be a lot easier to traverse.
Probably a mistake of his or something very specific on his part.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.