0

This is my JSON file that contains multiple objects of "Application":

{"Application":[{"appid":"0","appname":"application0"},
                {"appid":"1","appname":"application1"},
                ....
               ]} 

I'm receiving it from Android code into my WCF REST service method:

[WebInvoke(Method = "POST", UriTemplate = "/AcceptApp", RequestFormat = WebMessageFormat.Json,  
    ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
[OperationContract]
string AcceptApplication(Stream jsonstring);

And here's method definition:

public string AcceptApplication(Stream inputStream)
{
    StreamReader r = new StreamReader(inputStream);
    string jsonstring = r.ReadToEnd();
    try
    {
       List<ApplicationEntity> list = JsonConvert.DeserializeObject<List<ApplicationEntity>>(jsonstring);
       for (int i = 0; i < list.Count; i++)
       {
         // using data
       }
    }
    catch (Exception E)
    {
        Logger.Error(E.Message);
    }

My ApplicationEntity:

public class ApplicationEntity
{
    public string appid { get; set; }
    public string appname { get; set; }
} 

I'm getting jsonstring , but error I'm getting:

Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g.    
{"name":"value"}) into type  
'System.Collections.Generic.List`1[ApplicationEntity]' because the  
type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so
that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array 
or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type 
to force it to deserialize from a JSON object.

1 Answer 1

2

The JSON string you are trying to parse is not a list or an array. It is an object with a property called "Application" that is an array. Try this:

public class ApplicationObject
{
    public List<ApplicationEntity> Application { get; set; }
}
...
var apps = JsonConvert.DeserializeObject<ApplicationObject>(jsonstring);

Now you can access the list on apps.Application.

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

4 Comments

It gave me error: Object reference not set to an instance of an object. foreach (var item in apps.Applications) { try { ApplicationEntity ent = new ApplicationEntity(); ent = item; Status = InsertInstalledApps(ent); } catch (Exception ex) { Logger.Error( ex); } }
You do not need to create a ApplicationEntity instance, the "item" on your foreach alreay is a ApplicationEntity instance. So you just need to call Status = InsertInstalledApps(item); Are you sure the exception is not happening inside that method?
No Error is from same line: var apps = JsonConvert.DeserializeObject<ApplicationObject>(jsonstring); I even added : ApplicationObject apps = new ApplicationObject(); Before that line. But no luck.
Sorry about that - try to change the property name from Applications to Application and see if it works. And you do not need to create ApplicationObject, DeserializeObject() will do it for you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.