Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

json.net won't deserialize string

Posted on 2014-10-23
3
Medium Priority
?
1,588 Views
Last Modified: 2014-10-25
here is my class:
    public class FeedStatusResultMain
    {

        public long feedstatusid { get; set; }
        public long feedid { get; set; }
        public long clientid { get; set; }
        public string clientname { get; set; }
        public string feedname { get; set; }
        public string feeddescription { get; set; }
        public string direction { get; set; }
        public string status { get; set; }
        public string starttime { get; set; }
        public string endtime { get; set; }
        public string nextrundate { get; set; }
        public int summaryprocessed { get; set; }
        public int summaryloaded { get; set; }
        public int summaryrejected { get; set; }
        public string environment { get; set; }
    }

Here is the json response returned from my web service:
"[{
"feedstatusid":92738,
"feedid":143,
"clientid":2,
"clientname":"client1",
"feedname":"report1",
"feeddescription":"report1",
"direction":"O",
"status":"P",
"starttime":"2014-10-23 10:40:32",
"endtime":"2014-10-23 10:40:37",
"nextrundate":"2014-10-23 10:50:32",
"summaryprocessed":3,
"summaryloaded":0,
"summaryrejected":0,
"environment":""
},
{
"feedstatusid":92737,
"feedid":148,
"clientid":2,
"clientname":"client2",
"feedname":"report2",
"feeddescription":"report2",
"direction":"O",
"status":"P",
"starttime":"2014-10-23 10:40:00",
"endtime":"2014-10-23 10:40:07",
"nextrundate":"2014-10-23 10:50:00",
"summaryprocessed":2,
"summaryloaded":3,
"summaryrejected":0,
"environment":""
}]"

I try to deserialize into a list<FeedStatusResponseMain> like this:
List<FeedStatusResultMain> frx = JsonConvert.DeserializeObject<List<FeedStatusResultMain>>(rx);

it throws the following error:
System.ArgumentException: Could not cast or convert from System.String to System.Collections.Generic.List`1[TMSWebServiceClient.FeedStatusResultMain].

I am going nuts trying to find out why this error is happening.  I've tried the JArray.parse() and JObject.parse() methods, but nothing works, just errors.  I don't see anything wrong with the json and the code is right out of their documentation!
0
Comment
Question by:dhenderson12
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
ste5an earned 2000 total points
ID: 40399742
The problem is the first and last charachter in the JSON. These extra quotation marks create a different kind of object. Remove these two quotation marks, then it works.

namespace Samples
{
    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.IO;

    public class Sample
    {
        public static void Main(string[] args)
        {
            using (StreamReader sr = new StreamReader(@"C:\Temp\json.txt"))
            {
                string json = sr.ReadToEnd();                
                List<FeedStatusResultMain> elements = JsonConvert.DeserializeObject<List<FeedStatusResultMain>>(json);
                foreach (FeedStatusResultMain element in elements )
                {
                      Console.WriteLine(element.ToString());
                }
            }
            
            Console.ReadLine();
        }
    }

    public class FeedStatusResultMain
    {
        public long feedstatusid { get; set; }
        public long feedid { get; set; }
        public long clientid { get; set; }
        public string clientname { get; set; }
        public string feedname { get; set; }
        public string feeddescription { get; set; }
        public string direction { get; set; }
        public string status { get; set; }
        public string starttime { get; set; }
        public string endtime { get; set; }
        public string nextrundate { get; set; }
        public int summaryprocessed { get; set; }
        public int summaryloaded { get; set; }
        public int summaryrejected { get; set; }
        public string environment { get; set; }

        public override string ToString()
        {
            return this.feedname;
        }
    }
}

Open in new window

0
 

Author Comment

by:dhenderson12
ID: 40399938
OK!!  I also had to strip out the \ characters from the string ... is there a way to filter these out of the returned json string?  those are the \" double quote escape sequences.
0
 
LVL 35

Expert Comment

by:ste5an
ID: 40400308
There shouldn't be a need to do this. I would use Fiddler to control what is really sent to you. Those quotes may be from the  VS  debugger.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question