0

I have this Json String returned from a web api and stored in a var using JObject.Parse(result);

"{\"Status\":\"0\",\"Message\":\"OK\",\"Count\":\"2\",\"MethodLog\":[{\"ApplicationName\":\"MobileGeneral\",\"CompanySN\":\"0897001\",\"MethodLogCount\":12,\"MethodAccessRefusedCount\":0},{\"ApplicationName\":\"MobileGrain\",\"CompanySN\":\"0897123\",\"MethodLogCount\":900,\"MethodAccessRefusedCount\":0}]}"

I have already created required classes in my workspace:

{
public class MethodLogJson
{
    [JsonProperty("Status")]
    public string Status { get; set; }

    [JsonProperty("Message")]
    public string Message { get; set; }

    [JsonProperty("Count")]
    public string Count { get; set; }

    [JsonProperty("MethodLog")]
    public List<MethodLog> MethodLog { get; set; }
}
public class MethodLog
{
    [JsonProperty("ApplicationName")]
    public string ApplicationName { get; set; }

    [JsonProperty("CompanySN")]
    public string CompanySerial { get; set; }

    [JsonProperty("MethodLogCount")]
    public string MethodLogCnt { get; set; }

    [JsonProperty("MethodAccessRefusedCount")]
    public string MethodAccessRefusedCnt { get; set; }
}
}

In my controller I am using:

var resObj = JObject.Parse(result);
var methodLog = JsonConvert.DeserializeObject<MethodLogJson<MethodLog>>(resObj);

But it does not help. Shows me Non-generic Type cannot be used with type arguments. :( Help people!!

2
  • 1
    Well yes, MethodLogJson is a non-generic type - so MethodLogJson<MethodLog> makes no sense. Try just JsonConvert.DeserializeObject<MethodLogJson>(resObj) Commented Aug 21, 2014 at 13:37
  • @JonSkeet now it Shows: "the best overloaded method match has some invalid arguments." Commented Aug 21, 2014 at 13:41

2 Answers 2

3

You're passing a type argument to a class that doesn't take type arguments. Change

var methodLog = JsonConvert.DeserializeObject<MethodLogJson<MethodLog>>(resObj);

to

var methodLog = JsonConvert.DeserializeObject<MethodLogJson>(resObj);
Sign up to request clarification or add additional context in comments.

4 Comments

Shows me : the best overloaded method match has some invalid arguments.
What does the code for your controller action look like?
@Ninja9 You need to pass the JSON string to the JsonConvert.DeserializeObject and not the JObject (see the answer posted by @parachutinturtle)
Thank you @Bun I have already implemented parachutingturtle 's code in my controller and that is exactly what I want. thank you everyone!
1

You don't need the JObject.Parse, simply deserialize the string into a MethodLogJson without type parameters.

MethodLogJson m = JsonConvert.DeserializeObject<MethodLogJson>(result);

Comments

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.