C# 如何在C中迭代JSON并将每个对象添加到列表中?

C# 如何在C中迭代JSON并将每个对象添加到列表中?,c#,json,xamarin,C#,Json,Xamarin,我有一个JSON格式: { "id1": { "id": "183", "contentname": "Title", "content": "Some text", "created": "2020-07-27" }, "id2": { &quo

我有一个JSON格式:

{
  "id1": {
    "id": "183",
    "contentname": "Title",
    "content": "Some text",
    "created": "2020-07-27"
  },
  "id2": {
    "id": "182",
    "contentname": "Title",
    "content": "Some text",
    "created": "2020-07-23"
  },
  "id3": {
    "id": "180",
    "contentname": "Title",
    "content": "Some text",
    "created": "2020-07-20"
  },
  "id4": {
    "id": "179",
    "contentname": "Title",
    "content": "Some text",
    "created": "2020-07-19"
  }
}
我需要遍历它并将每个项目添加到新的ArrayList:

ArrayList NewList=新的ArrayList

我需要它,这样我以后就可以在ArrayList中循环并检索我想要的任何对象。 我尝试使用此网站,但它返回以下内容:

public class Id1    {
    public string id { get; set; } 
    public string contentname { get; set; } 
    public string content { get; set; } 
    public string created { get; set; } 
}

public class Id2    {
    public string id { get; set; }  
    public string contentname { get; set; } 
    public string content { get; set; } 
    public string created { get; set; } 
}

public class Root    {
    public Id1 id1 { get; set; } 
    public Id2 id2 { get; set; } 
}
这对我来说不是很有用,因为将会有100多个不同的ID,并且每天都会添加更多的ID

有什么办法可以满足我的要求吗?这是为了我的Xamarin项目,在Android中我使用了Volley库,它工作得很好,但我一直在C中苦苦挣扎

感谢您的帮助:

编辑: 这是生成我的json的PHP:

<?php
class SeznamNovinekApi extends BaseApi
{
    public function ToProcess($parametry)
    {
        $novinkyInfo = Database::SqlGetFetchAll("select id, pageid, contentname, content, created from mainpagesarticles where pageid = ? order by created desc", array('novinky'));

        $i = 0;
        foreach ($novinkyInfo as $info)
        {
            $i++;
            $obj = ["id" => $info["id"], "pageid" => $info["pageid"], "contentname" => $info["contentname"], "content" => $info["content"], "created" => $info["created"]];
            $this->json['id' . $i] = $obj;
        }
    }
}
?>

理想情况下,JSON应该更像这样:

{[
  {
    "id": "183",
    "contentname": "Title",
    "content": "Some text",
    "created": "2020-07-27"
  },
  {
    "id": "182",
    "contentname": "Title",
    "content": "Some text",
    "created": "2020-07-23"
  },
  {
    "id": "180",
    "contentname": "Title",
    "content": "Some text",
    "created": "2020-07-20"
  },
  {
    "id": "179",
    "contentname": "Title",
    "content": "Some text",
    "created": "2020-07-19"
  }
]}
只是JSON对象的普通数组。您不需要外部标识符,但需要数组。您以前的JSON是一个包含多个子对象的单一对象

使用此代码和伪代码,任何JSON转换器的工作原理如下:

MyType[] arrayOfObjects = JSON.Convert<MyType>(myJson);

理想情况下,JSON应该更像这样:

{[
  {
    "id": "183",
    "contentname": "Title",
    "content": "Some text",
    "created": "2020-07-27"
  },
  {
    "id": "182",
    "contentname": "Title",
    "content": "Some text",
    "created": "2020-07-23"
  },
  {
    "id": "180",
    "contentname": "Title",
    "content": "Some text",
    "created": "2020-07-20"
  },
  {
    "id": "179",
    "contentname": "Title",
    "content": "Some text",
    "created": "2020-07-19"
  }
]}
只是JSON对象的普通数组。您不需要外部标识符,但需要数组。您以前的JSON是一个包含多个子对象的单一对象

使用此代码和伪代码,任何JSON转换器的工作原理如下:

MyType[] arrayOfObjects = JSON.Convert<MyType>(myJson);

根据问题下面的注释,看到您有能力修复Json,我建议您首先将Json修复为类似这样的东西,它有一个实际的id元素数组

{
  "ids": [
    {
      "id": "183",
      "contentname": "Title",
      "content": "Some text",
      "created": "2020-07-27"
    },
    {
      "id": "182",
      "contentname": "Title",
      "content": "Some text",
      "created": "2020-07-23"
    },
    {
      "id": "180",
      "contentname": "Title",
      "content": "Some text",
      "created": "2020-07-20"
    },
    {
      "id": "179",
      "contentname": "Title",
      "content": "Some text",
      "created": "2020-07-19"
    }
  ]
}
一旦你有了这个顺序,VisualStudio中有一个非常漂亮的工具来帮助你基于这个JSON构建类定义。将json复制到剪贴板并复制到:编辑->粘贴特殊->将json粘贴为类。您可以在打开*.cs文件时执行此操作

这将为您构建以下JSON

public class Ids
{
    public Id[] ids { get; set; }
}

public class Id
{
    public string id { get; set; }
    public string contentname { get; set; }
    public string content { get; set; }
    public string created { get; set; }
}
在此之后,整个过程非常简单:

private static void Main(string[] args)
{
    var json = File.ReadAllText("test.json");
    var ids = JsonSerializer.Deserialize<Ids>(json);
    Console.WriteLine(ids);
}

根据问题下面的注释,看到您有能力修复Json,我建议您首先将Json修复为类似这样的东西,它有一个实际的id元素数组

{
  "ids": [
    {
      "id": "183",
      "contentname": "Title",
      "content": "Some text",
      "created": "2020-07-27"
    },
    {
      "id": "182",
      "contentname": "Title",
      "content": "Some text",
      "created": "2020-07-23"
    },
    {
      "id": "180",
      "contentname": "Title",
      "content": "Some text",
      "created": "2020-07-20"
    },
    {
      "id": "179",
      "contentname": "Title",
      "content": "Some text",
      "created": "2020-07-19"
    }
  ]
}
一旦你有了这个顺序,VisualStudio中有一个非常漂亮的工具来帮助你基于这个JSON构建类定义。将json复制到剪贴板并复制到:编辑->粘贴特殊->将json粘贴为类。您可以在打开*.cs文件时执行此操作

这将为您构建以下JSON

public class Ids
{
    public Id[] ids { get; set; }
}

public class Id
{
    public string id { get; set; }
    public string contentname { get; set; }
    public string content { get; set; }
    public string created { get; set; }
}
在此之后,整个过程非常简单:

private static void Main(string[] args)
{
    var json = File.ReadAllText("test.json");
    var ids = JsonSerializer.Deserialize<Ids>(json);
    Console.WriteLine(ids);
}

非常感谢每一位回复的人,现在mjwills的Quicktype代码可以工作了,但是我可能只需要修复json,然后正确地完成它:谢谢

非常感谢每一位回复的人,现在mjwills的Quicktype代码可以正常工作了,但我可能只需要修复json,并正确地完成它:谢谢

我使用的正是你的json

创建模型:

public class Data
{
    public int Id { get; set; }
    public string ContentName { get; set; }
    public string Content { get; set; }
    public DateTime Created { get; set; }
}
打开命名空间:

using Newtonsoft.Json.Linq;
使用此代码:

var text = File.ReadAllText("test.json");
var json = JObject.Parse(text);
var list = new List<Data>();

foreach (var prop in json.Properties())
{
    var value = prop.Value;
    var data = new Data
    {
        Id = value["id"].Value<int>(),
        ContentName = value["contentname"].ToString(),
        Content = value["content"].ToString(),
        Created = value["created"].Value<DateTime>()
    };
    list.Add(data);
}

我用的正是你的json

创建模型:

public class Data
{
    public int Id { get; set; }
    public string ContentName { get; set; }
    public string Content { get; set; }
    public DateTime Created { get; set; }
}
打开命名空间:

using Newtonsoft.Json.Linq;
使用此代码:

var text = File.ReadAllText("test.json");
var json = JObject.Parse(text);
var list = new List<Data>();

foreach (var prop in json.Properties())
{
    var value = prop.Value;
    var data = new Data
    {
        Id = value["id"].Value<int>(),
        ContentName = value["contentname"].ToString(),
        Content = value["content"].ToString(),
        Created = value["created"].Value<DateTime>()
    };
    list.Add(data);
}

您可以控制JSON吗?因为你的JSON不是一个对象数组。是的,我是这样做的,但它是用PHP编写的,所以我不太确定该怎么做:/你认为我必须改变我的整个JSON才能与C兼容吗?你最好修复你的JSON。你能发布生成这个的PHP代码吗?你正在构建一个关联数组,而实际上并不需要它。相反,只需构建一个简单的数据数组,然后返回该数组的json。您是否可以控制json?因为你的JSON不是一个对象数组。是的,我是这样做的,但它是用PHP编写的,所以我不太确定该怎么做:/你认为我必须改变我的整个JSON才能与C兼容吗?你最好修复你的JSON。你能发布生成这个的PHP代码吗?你正在构建一个关联数组,而实际上并不需要它。相反,只需构建一个简单的数据数组,然后返回该数组的json,因为OP需要一个只不过是其他对象数组的对象是有原因的。这对我来说是一种代码味道,在父类中没有其他有用的东西,特别是ID的名称与Int64不同。我相信OP有潜力根据其用例的范围找出更好的属性命名。这不是这个anwser的目标。假设有某种原因OP想要一个只不过是其他对象数组的对象。这对我来说是一种代码味道,在父类中没有其他有用的东西,特别是ID的名称与Int64不同。我相信OP有潜力根据其用例的范围找出更好的属性命名。这并不是这个安瑟的目标。