1

I'm getting the mentioned error when trying to DeserializeObject() into a list of strings.
ResultSet is fine and making a string as:
  string sRetVal_Json = new JavaScriptSerializer().Serialize(ResultSet);
also works fine.
The resultant string, e.g., is:
  sRetVal_Json = "[{\"CUSTNUM\":\"8690\"}]"

Here is a code snip:

    var ResultSet = (from cms in MASadminE.MOM_CMS
                     where cms.ORDER == sOrdNum
                     select new
                     {
                       cms.CUSTNUM
                     });
    List<string> list = new List<string>();
    string sRetVal_Json = new JavaScriptSerializer().Serialize(ResultSet);
    if (sRetVal_Json != "[]") // got > 0 records
    {
      list = JsonConvert.DeserializeObject<List<string>>(sRetVal_Json);
    }
Gary Huckabone
  • 402
  • 3
  • 10

3 Answers3

0

You're trying to deserialize a serialized list of anonymously-typed objects to a list of strings. The parser chokes when it hits the colon after the first property name. I don't think there's a way to use NewtonSoft to deserialize to a list of an anonymous type. You can deserialize to a Dictionary<string, string>, though:

Dictionary<string, string>> dict = JsonConvert.DeserializeObject<Dictionary<string, string>>>(sRetVal_Json);
D Stanley
  • 149,601
  • 11
  • 178
  • 240
0

It doesn't make sense to serialize and then deserialize to get your list of strings. You still have the same problem and you're just doing unecessary processing.

Change your projection to just select the value if that is all you want. Then ToList() it if there are any items in the sequence:

var ResultSet = (from cms in MASadminE.MOM_CMS
                     where cms.ORDER == sOrdNum
                     select cms.CUSTNUM);
if(ResultSet.Any()) 
{
    List<string> custnums = ResultSet.ToList();
}
Crowcoder
  • 11,250
  • 3
  • 36
  • 45
0

The issue is that the ResultSet here is actually the "dynamic" type because you created an anonymous class when you did

                     select new
                     {
                       cms.CUSTNUM
                     }

Thus there are 2 ways to do this. Either change your select to select back cms.CUSTNUM directly to get a list of strings (but JSON won't have CUSTNUM as a property) or you create a class that will support you here.

1.

 var ResultSet = (from cms in MASadminE.MOM_CMS
                     where cms.ORDER == sOrdNum
                     select  cms.CUSTNUM).ToList();

(Then you can use List< string > here)

2.

public class MyData
{
    public string CUSTNUM { get; set; }
}


var ResultSet = (from cms in MASadminE.MOM_CMS
                 where cms.ORDER == sOrdNum
                 select new MyData
                 {
                   CUSTNUM = cms.CUSTNUM
                 });
List<MyData> list = new List<MyData>();
string sRetVal_Json = new JavaScriptSerializer().Serialize(ResultSet);
if (sRetVal_Json != "[]") // got > 0 records
{
  list = JsonConvert.DeserializeObject<List<MyData>>(sRetVal_Json);
}
Daniel Lorenz
  • 4,178
  • 1
  • 32
  • 39