0

I have a problem, I cannot add custom objects to my List. However, if I add some predefined objects (strings, integers..) my code works just fine. This is my class which is always returned on all ajax calls to this controller, it contains boolean Status and List of objects.

    public class returnData
    {
        public bool status { get; set; }
        public List<object> data { get; set; }
    }

This is the class for the object I want to add to this list:

public class file
{
    public string ID { get; set; }
    public string Name { get; set; }
    public string Type { get; set; }
}

The code where I use this:

returnData data = new returnData();
...
DriveFile file = JsonConvert.DeserializeObject<DriveFile>(Json);

file f = new file();
if (file.mimeType == "application/vnd.google-apps.folder")
{
    f.Type = "folder";
    f.ID = file.id;
    f.Name = file.title;
    data.data.Add(f);
}

The error I get has no useful info, as this code compiles and the error is in Chrome Console log when the ajax is executed.

Image of google chrome console

However, when I use this line of code

data.data.add("some string");

my function will work flawlessly and return Object which contains boolean Status and List of objects (strings, integers..)

So back to my question, how do I add my objects to this list?

EDIT: There are no server side errors, the code compiles and runs just fine, so this probably is some jquery/ajax error.

This is the ajax code for my debugger:

86    $.ajax({
87  url: $('#url').val(),
88  type: $('#type option:selected').val(),
89  contentType: "application/json; charset=utf-8",
90  dataType: "json",
91  data: $('#data').val(),
92  success: function (result){
93  console.log(result)
94  },
95  error: function (err){
96  console.log(err)
97  }                   
98    });

Here is the complete method, but the method seems to be OK, it is the ajax or something to do with jquery I guess.

[OperationContract]
    public returnData listChildren(string folderId)
    {
        returnData data = new returnData();
        List<object> myList= new List<object>();

        bool status = true;
        try
        {
            string Json = Response("https://www.googleapis.com/drive/v2/files/" + folderId + "/children", "GET");
//get the children list
            if (!Json.Contains("error"))
            {
                DriveChildlist list = JsonConvert.DeserializeObject<DriveChildlist>(Json);
                foreach (DriveChildren child in list.items)
                {
                    string uriString = child.childLink;
//get the info for each child in the list
                    Json = Response(uriString, "GET");
                    if (!Json.Contains("error"))
                    {
                        DriveFile file = JsonConvert.DeserializeObject<DriveFile>(Json);
                        file f = new file();
                        if (file.mimeType == "application/vnd.google-apps.folder")
                        {
                            f.Type = "folder";
                            f.ID = file.id;
                            f.Name = file.title;
                            data.data.Add(f);

                        }
                        else if (file.mimeType == "application/myMimeType")
                        {
                            f.Type = "file";
                            f.ID = file.id;
                            f.Name = file.title;
                            data.data.Add(f);

                        }


                    }
                    else
                    {
                        status = false;
                    }

                }

            }
            else
            {
                status = false;
            }
        }
        catch(Exception)
        {
            status = false;
        }
        data.status = status;
        if (!status)
        {

            data.data = null;
//data.data is null if status is false
        }
        return data;
    }

You can save the typed in value in the maxm variable. If the user types a larger number, then you replace that value:

static void Main(string[] args)
{
    int i, pcm = 0, maxm = 0;
    for (i = 1; i <= 3; i++)
    {
        Console.WriteLine("Please enter your computer marks");
        pcm = int.Parse(Console.ReadLine());

        // logic to save off the larger of the two (maxm or pcm)
        maxm = maxm > pcm ? maxm : pcm;
    }
    Console.WriteLine(string.Format("The max value is: {0}", maxm));
    Console.ReadKey();
}
4

2 回答 2

0

我已经使用 Newtonsoft.Json 解决了这个问题。我已经使用以下代码序列化了对象:

string jsondata = JsonConvert.SerializeObject(data);

然后我返回序列化字符串而不是对象。看起来 Visual Studio 开发中心未能序列化我的对象,即使在我的课程中添加了 [Serializable()] 也是如此。有这方面的信息吗?

于 2013-10-05T09:37:39.337 回答
0

正如所写的那样,您的 C# 代码将不起作用,但会编译。调用 ajax 时会抛出错误,您可能永远不会看到错误消息(取决于您的系统设置方式)。

这是正确的代码(给定您的代码段)

returnData data = new returnData();
data.data = new List<object>();
// rest of your code.
于 2013-10-05T08:08:50.173 回答