0

I am doing a WebApi Method in Visual Studio 2013 and I want to Deserialize a Class Type. My Class is like this

[JsonObject]
    class JOTA
    {
        [JsonProperty("ProductId")]
        public int ProductId { get; set; }
        [JsonProperty("Name")]
        public string Name { get; set; }
    }

My call is like this.

 public void ReturnListProd(JOTA PP)
 {
 JOTA product = Newtonsoft.Json.JsonConvert.DeserializeObject<JOTA>(PP);
 }

I have a compile error

'Network.Json.Json.Converter[] has some invalid argument'

But, if a define an ArrayList

public void ReturnListProd(ArrayList PP)
{
JOTA product = Newtonsoft.Json.JsonConvert.DeserializeObject<JOTA>(PP[0].ToString());
}

I have no error. But in this case, it does not help on what I need.

What I am missing? Thanks

2
  • 1
    The first example doesn't make sense, why would you want to take a JOTA object (not a serialized version) and deserialize it to a JOTA object again? In that case, your method body would simply be JOTA product = PP; Also, why do your methods return void, you are just throwing away the result? The second example isn't a compile time error, but I'm betting it would be a run-time one. Commented Oct 30, 2015 at 15:05
  • 1
    The DeserializeObject method takes a string as a parameter but you are giving it a JOTA object. The error tells you exactly what the problem is. Commented Oct 30, 2015 at 15:10

2 Answers 2

3

If you want a JOTA object to become a string representation of itself (serialize it) then you should be using

string serialized = Newtonsoft.Json.JsonConvert.SerializeObject(PP);

If you want the string to become a JOTA object then you are using

JOTA product = Newtonsoft.Json.JsonConvert.DeserializeObject<JOTA>(serialized);

the problem is that you are trying to deserialize an object that is not serialized (already deserialized).

Sign up to request clarification or add additional context in comments.

Comments

1

You don't need the attributes if the property names are not different.

public class JOTA
{
    public int ProductId { get; set; }
    public string Name { get; set; }
}

public void ReturnListProd(JOTA PP)
{
   var product = PP; //not really needed you can just use PP directly
}

You only need to deserialize if you are receiving a json string. Because you are using WebAPI I would suggest changing your API endpoint to a proper REST endpoint. Example:

[HttpPost, Route("api/products/add")]
public IHttpActionResult ReturnListProd([FromBody]JOTA PP)
{
    try
    {
        //do something with passed in data
        var name = PP.Name;

        //always at minimum return a status code
        return Ok();
    }
    catch
    {
        //returns 500
        return InternalServerError();
    }
}

Then change your ajax url from: url: "yourController/ReturnListProd" to: url: "/api/products/add"

2 Comments

thanks... I thought I need to Deserealize becouse I pass it as a parameter from Client. It Works...!!
No problem. One of the nice things about webApi is that it handles the serialization for you. Since you are using webApi I'm going to update my answer with some suggestions.

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.