【问题标题】:Updation of Dictionary Key Values which are in JSON array / ArrayLIst C#更新 JSON 数组/ArrayLIst C# 中的字典键值
【发布时间】:2018-04-16 01:58:25
【问题描述】:

需要使用下面显示的相同数据更新ArrayListarlData,但需要删除除前1 之外的albumName 值,即albumName[0] 的值,Json 数据应更新,如下面的输出所示。

static void Main(string[] args)
{
    /*Declaration */
    ArrayList arlData = null;
    arlData = new ArrayList();
    Dictionary<string, object> data = null;

    //Json data format
    string json = @"{""Id"":""1"",""Count"":""2"",""musicName"":""test1"",""albumName"":[""1"", ""2"",""3""]}"
          ,json2 = @"{""Id"":""2"",""Count"":""1"",""musicName"":""test2"",""albumName"":[""4"", ""5""]}"
          ,json3 = @"{""Id"":""3"",""Count"":""1"",""musicName"":""test3"",""albumName"":[""6"", ""7""]}";

    /*Adding data to array list   */
    arlData.Add(JsonConvert.DeserializeObject<Dictionary<string, object>>(json));
    arlData.Add(JsonConvert.DeserializeObject<Dictionary<string, object>>(json2));
    arlData.Add(JsonConvert.DeserializeObject<Dictionary<string, object>>(json3));
    int intbytlen = 0;

    if (arlData.Count > 0)        
    {
        intbytlen = arlData.Count;                
        for (int iterator = 0; iterator < intbytlen; iterator++)
        {
            //Data fetch
            data = (Dictionary<string, object>)arlData[iterator];

            //Data serialization
            Console.WriteLine(JsonConvert.SerializeObject(data, Formatting.Indented));
        }
    }
}

输出应该是:

{"Id":"1","Count":"2","musicName":"test1","albumName":["1"]}
{"Id":"2","Count":"1","musicName":"test2","albumName":["4"]}
{"Id":"3","Count":"1","musicName":"test3","albumName":["6"]}

【问题讨论】:

  • 我会考虑使用库来构建您的 JSON。 Newtonsoft,在我看来是最好的之一。我还将创建一个代表您的 json 字符串的对象,并根据需要进行序列化/反序列化,然后您可以轻松地对对象进行更改。
  • 您能否简要介绍一下创建对象并更新反序列化值&我正在使用Newtonsoft.Json
  • 当然,您将创建模型(类),并且 albumName 属性将返回一个 List。然后,当您需要进行更改时,您将反序列化您的对象,访问 List 并删除除索引 0 之外的所有项目。然后再次序列化。

标签: c# .net json arraylist


【解决方案1】:

考虑使用与此类似的模型。

public class AlbumList
{      
    public int ID { get; set; }
    public int Count { get; set; }
    public string MusicName { get; set; }
    public List<Album> AlbumName { get; set; }
}

public class Album
{
    public int AlbumName { get; set; }
} 

然后用数据实例化和填充对象,并添加到List&lt;AlbumList&gt;。然后,您可以在需要时使用 Newtonsoft 进行序列化和反序列化。

var albums = JsonConvert.DeserializeObject<List<AlbumList>>(json);

var json = JsonConvert.SerializeObject(albums, Formatting.Indented);

反序列化后,访问 List&lt;AlbumList&gt; 并删除所有非索引 0 的项目。

for (int i = 1; i < AlbumList.Count; i++)
            {
                AlbumList.RemoveAt(i);
            }

【讨论】:

    【解决方案2】:

    我找到了最简单的方法,无需创建类和属性来更新字典键值和 JSON 数据,只需使用 type dynamic 从数组列表中获取数据!

    if (data.ContainsKey("albumName") && data["albumName"] != null)
     {
       //Assign specific data to type dynamic by deserialising the data
       dynamic arryAlbumName = JsonConvert.DeserializeObject(data["albumName"].ToString());
       if (arryAlbumName != null && arryAlbumName.Count > 0)
       {
        for (int count = 0; count < arryAlbumName.Count; count++)
        {
          if (count == 0)
          {
            ArrayList albumName = new ArrayList();
            //Check for data exists and update the array value to arlData[]
            if (!string.IsNullOrEmpty(arryAlbumName[count].ToString()))
            {
                albumName.Add(arryAlbumName[count]);
                data.Remove("albumName");
                data.Add("albumName", albumName);
                arlData[iterator] = data;
             }
            }
          } 
        }
      } 
    

    输出是

     {"Id":"1","Count":"2","musicName":"test1","albumName":["1"]}
     {"Id":"2","Count":"1","musicName":"test2","albumName":["4"]}
     {"Id":"3","Count":"1","musicName":"test3","albumName":["6"]}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-28
      • 1970-01-01
      • 1970-01-01
      • 2021-09-16
      • 1970-01-01
      • 2016-08-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多