开发者

Windows Phone 7 JSON parsing error

I have this JSON string

{ "rs:data": { "z:row": [ {"Lead": "", "Industry": "Other Commercial", "ID": "908", "Name": "3PAR Ltd." }, {"Lead": "Ebeling, Kevin R.", "Industry": "Retail", "ID": "1", "Name": "7-Eleven" } ] }}

Now I am getting data in the above format from a web service into Win phone 7. But while trying to parse I am facing a error:

    void fetcher_GetClientsCompleted(object sender, ServiceReference2.GetClientsCompletedEventArgs e)
    {
        StringReader reader;
        reader = new StringReader(e.Result);
        IList<Clientclass> cc;
        string MyJsonString = reader.ReadToEnd(); //

        cc = Deserialize(MyJsonString); 
    }


    public static IList<Clientclass> Deserialize(string json)
    {
        using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
        {
            var serializer = new DataContractJsonSerializer(typeof(IList<Clientclass>));
            return (IList<Clientclass>)serializer.ReadObject(ms);
        }
    }

My data has to parsed as per clientclass, where clientclass is:

    public class Clientclass
    {
        string _id;
        st开发者_开发技巧ring _name;
        string _industry;
        string _lead;

        public string ID
        {
            get { return _id; }
            set { _id = value; }
        }
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        public string Industry
        {
            get {return _industry; }
            set { _industry= value; }
        }
        public string Lead
        {
            get { return _lead; }
            set { _lead = value; }
        }
    }

Please note there are more than one records in JSON string.

Thanks santu


You are deserializing the wrong type (IList instead of List of ClientClass - the original post had typeof(IList) however Chris corrected this as part of his code formatting edits). You should be doing:

public static List<Clientclass> Deserialize(string json)
{
    using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
    {
        var serializer = new DataContractJsonSerializer(typeof(List<Clientclass>));
        return (List<Clientclass>)serializer.ReadObject(ms);
    }
}

Also, your JSON includes stuff you are not decoding. If you want to use the above method, your JSON would have to be:

[ {"Lead": "", "Industry": "Other Commercial", "ID": "908", "Name": "3PAR Ltd." }, {"Lead": "Ebeling, Kevin R.", "Industry": "Retail", "ID": "1", "Name": "7-Eleven" } ]

In order to decode the string from your original post, you'll need a containing class, and define DataMember attributes to handle the names:

    public class ClassA
    {
        [DataMember(Name = "rs:data")]
        public ClassB Data { get; set; }
    }

    public class ClassB
    {
        [DataMember(Name="z:row")]
        public List<Clientclass> Row { get; set; }
    }

Then deserialize ClassA:

    public static ClassA Deserialize(string json)
    {
        using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
        {
            var serializer = new DataContractJsonSerializer(typeof(ClassA));
            return (ClassA)serializer.ReadObject(ms);
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜