Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否需要从DocumentDb反序列化查询结果?_C#_Asp.net Mvc_Json_Serialization_Azure Cosmosdb - Fatal编程技术网

C# 是否需要从DocumentDb反序列化查询结果?

C# 是否需要从DocumentDb反序列化查询结果?,c#,asp.net-mvc,json,serialization,azure-cosmosdb,C#,Asp.net Mvc,Json,Serialization,Azure Cosmosdb,我有一个简单的POCO for person对象,如下所示: public class Person { public int PersonId {get; set;} public string FirstName {get; set;} public string MiddleName {get; set;} public string LastName {get; set;} public char Gender {get; set;} } 我有以下查询Do

我有一个简单的POCO for person对象,如下所示:

public class Person
{
   public int PersonId {get; set;}
   public string FirstName {get; set;}
   public string MiddleName {get; set;}
   public string LastName {get; set;}
   public char Gender {get; set;}
}
我有以下查询DocumentDb数据库中人员集合的代码

private async static Task<List<Person>> GetPeopleList(string colSelfLink)
{
   dynamic doc = client.CreateDocumentQuery<Document>(colSelfLink, "SELECT p.PersonId, p.FirstName, p.MiddleName, p.LastName, p.Gender FROM People p").AsEnumerable().FirstOrDefault();
   List<Person> peopleList = new List<Person>();
   if (doc != null)
   {
      peopleList = doc;
   }
   return peopleList;
}
private异步静态任务GetPeopleList(字符串colSelfLink)
{
dynamic doc=client.CreateDocumentQuery(colSelfLink,“从人物p中选择p.PersonId、p.FirstName、p.MiddleName、p.LastName、p.Gender”).AsEnumerable().FirstOrDefault();
List peopleList=新列表();
如果(doc!=null)
{
peopleList=doc;
}
返回人员列表;
}
当我运行此代码时,出现以下错误:

无法将当前JSON对象(例如{“名称”:“值”})反序列化为类型“System.Collections.Generic.List`1[MyApp.Person]”,因为该类型需要JSON数组(例如[1,2,3])才能正确反序列化。若要修复此错误,请将JSON更改为JSON数组(例如[1,2,3]),或更改反序列化类型,使其成为可以从JSON对象反序列化的正常.NET类型(例如,不是integer之类的基元类型,也不是数组或列表之类的集合类型)。还可以将JsonObjectAttribute添加到类型中,以强制它从JSON对象反序列化。路径“PersonId”,第1行,位置 48


如何将来自查询的JSON对象转换为Person对象?在我的MVC应用程序中,model binder做得很好,但它在一个单独的类库中运行。我应该如何转换这个JSON对象?

这就是我所做的,它是有效的。如果有更好的方法,我会很感激其他的答案

private async static Task<List<Person>> GetPeopleList(string colSelfLink)
{
   dynamic doc = client.CreateDocumentQuery<Document>(colSelfLink, "SELECT p.PersonId, p.FirstName, p.MiddleName, p.LastName, p.Gender FROM People p").AsEnumerable().ToList();
   List<Person> peopleList = new List<Person>();
   if (doc != null)
   {
      Person person;
      foreach(var item in doc)
      {
         person = JsonConvert.DeserializeObject<Person>(item.ToString());
         peopleList.Add(person);
      }
   }
   return peopleList;
}
private异步静态任务GetPeopleList(字符串colSelfLink)
{
dynamic doc=client.CreateDocumentQuery(colSelfLink,“从People p中选择p.PersonId、p.FirstName、p.MiddleName、p.LastName、p.Gender”).AsEnumerable().ToList();
List peopleList=新列表();
如果(doc!=null)
{
个人;
foreach(单据中的var项目)
{
person=JsonConvert.DeserializeObject(item.ToString());
人员列表。添加(个人);
}
}
返回人员列表;
}

这一次,安东尼·朱(Anthony Chu)走上了正确的道路。简单的解决办法是:

private async static Task<List<Person>> GetPeopleList(string colSelfLink)
{
   return client.CreateDocumentQuery<Person>(colSelfLink, "SELECT * FROM People").ToList();
}
private异步静态任务GetPeopleList(字符串colSelfLink)
{
return client.CreateDocumentQuery(colSelfLink,“SELECT*FROM People”).ToList();
}

这将自动反序列化每个返回的记录,并将其转换为人员对象。

我遇到了完全相同的问题,只是我的对象列表失败,嵌套在另一个对象(文档)中


这是主对象(文档)上需要的,但在嵌套对象上不需要它们,直到它们出现在列表中。

FirstOrDefault()
返回单个对象(或null)。它的结果无法分配给列表。我明白你的意思,所以我将FirstOrDefault()更改为ToList(),但仍然存在问题。我现在得到“无法隐式地将类型'System.Collections.GenericList'转换为'System.Collections.GenericList'”错误。如何处理转换?我是否需要一个foreach和句柄来将每个文档转换为Person对象?我也试过了,但失败了。我想我成功了。看起来我需要执行foreach循环,并将document/json对象反序列化到我的POCO类中。这是正确的方法吗?这对我来说很有效,但我想确定没有更好的方法可以做到。我还没有使用DocumentDb,但我想这可能就是您所需要的全部了<代码>列表peopleList=client.CreateDocumentQuery(colSelfLink,“…”).AsEnumerable().ToList()我找到的最好的东西。我玩了几十种不同的方式,但都没有得到任何效果。我也这么做了,但我正在远离它。
.ToString()
的问题在于,DocumentDB SDK资源类重写(或不重写)了
.ToString()
。更好的方法是首先
JsonConvert.SerializeObject(item)
,然后将其反序列化到POCO。如果集合中有多种文档格式,该怎么办?这是完全可能的,因为在cosmos数据库中,集合基本上只是文档的“存储桶”。
public List<MyObject> MyProperty { get; set; }
[JsonProperty(PropertyName = "yourPropertyName")]