3

Here is my code:

Encoding enc = System.Text.Encoding.GetEncoding(1252);
StreamReader loResponseStream = new StreamReader(resp.GetResponseStream(), enc);
JsonSerializer serializer = new JsonSerializer();
JsonTextReader jsreader = new JsonTextReader(loResponseStream);
results = (mHealthData)serializer.Deserialize(jsreader, typeof(mHealthData)); ***
loResponseStream.Close();

public class mHealthData
{ // Class for the Mhealth Data
    public class RootObject
    {
        public string source { get; set; }
        public string name { get; set; }
        public string type { get; set; }
        public string unit { get; set; }
        public double value { get; set; }
        public string timestamp { get; set; }
    }
}   

This is the error I receive On the line marked with a **:

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'MHealthPlugin.mHealthData' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.

I have no clue how to fix this. I've tried putting List<mHealthData> practically everywhere in the code and it doesn't help. The other rest Calls I do work fine. For reference, here is an example of what the JSON I'm trying to parse looks like:

[{"source":"hi","name":"G","type":"number","unit":null,"value":126,"timestamp":"1974-07-27T09:35:12Z"},{"source":"hi","name":"G","type":"number","unit":null,"value":120,"timestamp":"1974-07-27T09:35:12Z"}]
5
  • you cant define a class as a list, right now, you just have a class named List<RootObject>. its not actually a list or related to a list at all. Commented Jun 13, 2013 at 19:54
  • FYI: If you use JSON.NET (aka Newtonsoft.Json), there are one-liners for serializing to/from a string: deserializing can be done with var x = JsonConvert.DeserializeObject<RootObject[]>(myJson);. Much nicer than dealing with all the streams and readers. Commented Jun 13, 2013 at 19:54
  • also, what type is your response variable? Commented Jun 13, 2013 at 19:55
  • My response variable is of type HTTPWebResponse named resp. I tried using ` var x = JsonConvert.DeserializeObject<mhealthData[]>(resp); ` but I get the error Error 11 The best overloaded method match for 'Newtonsoft.Json.JsonConvert.DeserializeObject<MHealthPlugin.mhealthData[]>(string, params Newtonsoft.Json.JsonConverter[])' has some invalid arguments Commented Jun 13, 2013 at 20:28
  • For the JsonConvert thing, you still need to start with a string. You can get the string from your HttpWebResponse object with a one-liner: string responseText = new StreamReader(resp.GetResponseStream()).ReadToEnd(); Commented Jun 13, 2013 at 21:57

1 Answer 1

3

Your mHealthData class doesn't have anything in it, except a nested class. Take a step back and see how your data is supposed to be defined.

It looks like you want a RootObject class. In that case, the class should be:

public class RootObject
{
    public string source { get; set; }
    public string name { get; set; }
    public string type { get; set; }
    public string unit { get; set; }
    public double value { get; set; }
    public string timestamp { get; set; }
}

Then when you deserialize, you'll end up with an object of type RootObject[] - your JSON is simply an array, not an object containing an array.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.