0

I have two types SalesData and CollectionData. These two types are entirely different type. So when I passed the data to front end i created dynamic data type and passed the data to front end depending on the call.

While receiving data back, I created json data which is array of objects.

Now I am in situation how do i assigned incoming arrays to List or List according to the type.

This is what I have done.

public ActionResult DownloadDataInExcel(List<object>data, int type)
        {
            if (type == (int)MyEnum.Sales)
            {
                var mycontacts = JsonConvert.DeserializeObject<List<SalesData>>(data.ToString());

                foreach (var item in mycontacts)
                {
                    System.Diagnostics.Debug.WriteLine($"{item.Name}-{item.City}-{item.Country}");
                }
            }else
            {
                var mycontacts = JsonConvert.DeserializeObject<List<CollectionData>>(data.ToString());

                foreach (var item in mycontacts)
                {
                    System.Diagnostics.Debug.WriteLine($"{item.Name}-{item.City}-{item.Country}");
                }
            }

            //to do: Convert data to Excel File

            throw new NotImplementedException();

        }
6
  • did u successfully deserialize your json to either of type SalesData or CollectionData? Commented Sep 25, 2018 at 6:12
  • No. I think issue is I am passing array of array from ajax. Commented Sep 25, 2018 at 6:14
  • Please write the JSON and class for SalesData and CollectionData. You might have issue in the structure. We can help if you give complete information Commented Sep 25, 2018 at 6:44
  • Problem was data I have been passing via ajax was causing the error. I passed json object after stringifying and received as string in my method. "public ActionResult DownloadDataInExcel(string data, int type)". Commented Sep 25, 2018 at 13:53
  • i want to ask one question is that, you dont know incoming json datatype and in action method you want to deserialize into either of type SalesData or CollectionData ? Commented Sep 25, 2018 at 13:55

2 Answers 2

1

1) You need a generic method to deserialize your incoming json to appropriate List<> object

public static List<T> DeserializeList<T>(string json)
{
    return JsonConvert.DeserializeObject<List<T>>(json);
}

2) Here I create an console app for your demonstration purpose.

class Program
{
    static void Main(string[] args)
    {
        string json1 = @"[{'Id1':'1','Name1':'Mike','Age1':43},{'Id1':'2','Name1':'Anna','Age1':56}]";
        string json2 = @"[{'Id2':'1','Name2':'Mike','Age2':43},{'Id2':'2','Name2':'Anna','Age2':56}]";

        //Pass json1 and deserialize into list of Sample1
        var sample1List = DeserializeList<Sample1>(json1);
        sample1List.ForEach(x => Console.WriteLine($"Id1: {x.Id1},  Name1: {x.Name1}, Age1: {x.Age1}"));

        Console.WriteLine("\n");

        //Pass json2 and deserialize into list of Sample2
        var sample2List = DeserializeList<Sample2>(json2);
        sample2List.ForEach(x => Console.WriteLine($"Id2: {x.Id2},  Name2: {x.Name2}, Age2: {x.Age2}"));

        Console.ReadLine();
    }

    public static List<T> DeserializeList<T>(string json)
    {
        return JsonConvert.DeserializeObject<List<T>>(json);
    }
}

class Sample1
{
    public string Id1 { get; set; }
    public string Name1 { get; set; }
    public int Age1 { get; set; }
}


class Sample2
{
    public string Id2 { get; set; }
    public string Name2 { get; set; }
    public int Age2 { get; set; }
}

Output:

enter image description here

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

1 Comment

@istudent, view the answer might be it help you :)
0

You can use try-catch

try
{
     var someresult = JsonConvert.DeserializeObject<Type1>(data.ToString());
     // data was Type1 indeed, deal with Type1
}
catch{} 
try
{
     var someresult = JsonConvert.DeserializeObject<Type2>(data.ToString());
     // data was Type2 indeed, deal with Type2
}
catch{} 

3 Comments

exception message is "Unexpected character encountered while parsing value: S. Path '', line 0, position 0."
well you would always have exception in one of these catches as the type would only match one. but if you are getting exception for both then your passed item does not match any of them.
I think i am getting exception as i was passing array of array via ajax. i will get back once i successfully create json object. Thank you Ashkan.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.