反序列化对象结果后有多余的括号

时间:2018-12-31 18:43:28

标签: c# json ajax post

这是我的数据来自ajax帖子。

string form = "[{\"id\": \"1\", \"name\": \"deneme\"},{\"id\": \"2\", \"name\": \"deneme2\"}]"

当我像这样反序列化时:

var data = JsonConvert.DeserializeObject(form);

对象是这样的:

{ [{"id": 1, "name": "deneme"},{"id": 2, "name": "deneme2"}] } 

我该如何解决这个问题?

编辑:

[HttpPost]
        public HttpResponseMessage ImportFromExcel(string form, string controller, string action)
        {
            //form = "[{\"id\": \"1\", \"name\": \"deneme\"},{\"id\": \"2\", \"name\": \"deneme2\"}]"
            ImportObject _importObject = new ImportObject();
            _importObject.SchemaName = controller;
            _importObject.TableName = action;

            var deger = JsonConvert.DeserializeObject(form);
            //deger = { [{"id": 1, "name": "deneme"},{"id": 2, "name": "deneme2"}] } 
            Utility.HttpPostJson(proUtil.GetConfigStr("ApiImportUrl", ""), "Import", JsonConvert.SerializeObject(_importObject));

            return new HttpResponseMessage(HttpStatusCode.Created);
        }

public class ImportObject
{
    public string SchemaName { get; set; }
    public string TableName { get; set; }
    public List<string> DataSet { get; set; }
}

2 个答案:

答案 0 :(得分:1)

因此,您正在反序列化为JSON.Net对象,而不是使用适当的具体类结构。我建议做这样的事情。首先创建一个类来保存您的数据:

public class FormItem
{
    public int Id { get; set; }
    public string Name { get; set; }
}

现在像这样反序列化为IEnumerable<FormItem>

var data = JsonConvert.DeserializeObject<IEnumerable<FormItem>>(form);

现在您可以像遍历其他所有枚举一样遍历它:

foreach(var formItem in data)
{
    Console.WriteLine($"Item with id of {formItem.Id} has a name of {formItem.Name}");
}

答案 1 :(得分:0)

解决方案是:

<Spinner
            android:id="@+id/BuildingSelect"
            style="@android:style/Widget.DeviceDefault.Light.Spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/colorAccent"
            android:dropDownWidth="match_parent"
            android:minWidth="224dp"/>