简体   繁体   English

如何修改列表中的值 <Dictionary<string,object> &gt;

[英]How to modify the value in List<Dictionary<string,object>>

In my project, the web browser send a json object back to server side(Server side I use Asp.net mvc and c#). 在我的项目中,Web浏览器将json对象发送回服务器端(服务器端我使用Asp.net mvc和c#)。 The json object like below: json对象如下所示:

[{
  id : "myid",
  name : "myname",
  type : "mytype",
  items : [{
   id: "item_id_1",
   name : "item_name_2",
   isInput : true,
   value : value
  },{
   id : "item_id_2",
   name : "item_name_2",
   type : "mytype",
   items : [...]
  }]
},{
  id : "myid",
  name : "myname",
  type : "mytype",
  items : [...]
}]

On server side I user parse the json string like this way: 在服务器端,我以这种方式解析json字符串:

JavaScriptSerializer jss = new JavaScriptSerializer();
List<Distionary<string,object>> jsonObj = (List<Distionary<string,object>>)jss.Deserialize(jsonString,typeof(List<Distionary<string,object>>));

My question is, how to modify the value in jsonObj. 我的问题是,如何修改jsonObj中的值。 For example, how can I modify the child object with the id "item_id_2". 例如,如何修改ID为“ item_id_2”的子对象。 I try the code below: 我尝试下面的代码:

KeyValuePair<string,object> itemObject = (KeyValuePair<string,object>)item.FirstOrDefault(d=>d.Key == "item_id_2");
itemOject.value = "XXX"

The result is I realy modify the object itemObject but the object in jsonObj has no change. 结果是我确实修改了对象itemObject,但是jsonObj中的对象没有更改。 So my question is how to modify the object in List>. 所以我的问题是如何在List>中修改对象。 Thanks. 谢谢。

ps: I also want to know after edit, how to convert the object to json string. ps:我还想知道编辑后如何将对象转换为json字符串。 Thanks. 谢谢。

ps: On client side, the function below build the object: ps:在客户端,下面的函数构建对象:

function toObj(item){
  var obj = item.toObj();
  if(item.items){
   obj.items = [];
   for(var i = 0;i<item.items.length;i++){
     obj.items.push(toObj(item.items[i]));
   }
  }
  return obj;
}

Create 2 custom objects like: 创建2个自定义对象,例如:

  public class Object1
   {
         public string id {get;set;}
         public string name {get;set;}
         public string type {get;set;}
         public List<Object2> items {get;set;}
   }

    public class Object2
    {
         public string id {get;set;}
         public string name {get;set;}
         public string value {get;set;}
         public bool isInput {get;set;}
    }

Deserialize the json: 反序列化json:

  JavaScriptSerializer jss = new JavaScriptSerializer();
  List<Object1> jsonObj = (List<Object1>>)jss.Deserialize(jsonString,typeof(List<Object1>))

Linq: Linq:

var obj= jsonObj.FirstOrDefault(c=>c.id="item_id_2");
objt.items[0].value ="change";

Don't rely on a List<Dictionary<String,Object>> , that is a hell to maintain, as evident in your problem. 不要依赖List<Dictionary<String,Object>> ,这很难维护,这在您的问题中很明显。 Define a class to hold your data, eg: 定义一个类来保存您的数据,例如:

public class SomeJsonObject
{
    public String id { get; set; }
    public String name { get; set; }
    public String type { get; set; }
    public bool isInput { get; set; }
    public String value { get; set; }
    public List<SomeJsonObject> items { get; set; }

    public IEnumerable<SomeJsonObject> AllWithChildren
    {
        get
        {
            yield return this;
            if (this.items == null) yield break;
            foreach (var descendant in
               this.items.SelectMany(child => child.AllWithChildren))
            {
                yield return descendant;
            }
        }
    }
}

The AllWithChildren helper property allows you to list all your items as a flat list. AllWithChildren帮助器属性使您可以将所有项目作为平面列表列出。 Then you can do something like: 然后,您可以执行以下操作:

var jsonObj = jss.Deserialize(jsonString, typeof(List<SomeJsonObject>))
                 as (List<SomeJsonObject>);
var item2 = jsonObj.SelectMany(x => x.AllWithChildren)
                   .FirstOrDefault(x => x.id == "item_id_2");
if (item2 != null) item2.value = "XXX";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查 List 类型的对象<Dictionary<string, string> &gt; 根据字典中的另一个值设置一个值? - How to check an object of type List<Dictionary<string, string>> to set one value based off another value in the dictionary? 字典&lt;(字符串,字符串,字符串),列表<object> &gt; 如何获取与此类字典的 1 个键匹配的值 - Dictionary<(string, string, string), List<object>> how to get value that match 1 key of this kind of dictionary 如何将我的y.Value投射到列表 <Dictionary<string, object> &gt; - how to cast my y.Value to List<Dictionary<string, object>> 在List中搜索值 <Dictionary<string, object> &gt; - Search for a value in List<Dictionary<string, object>> 如何转换字典 <object, string> 到字典 <object, List<string> &gt;使用LINQ - How to transform a Dictionary<object, string> to a Dictionary<object, List<string>> with LINQ 显示和修改字典 <string, List<string> &gt;使用DataGrid - Display and modify Dictionary<string, List<string>> with a DataGrid 如何转换列表<Claim>到字典<string,object> ? - How to convert List<Claim> to Dictionary<string,object>? 如何获得字典 <string, List<Object> 与LINQ? - How to get a Dictionary<string, List<Object> with LINQ? 如何转换字典 <string, object> 列出 <T> - How to convert Dictionary<string, object> to List<T> 在列表中搜索值<dictionary<string, object> > 但使用非特定字符串</dictionary<string,> - Search for a value in List<Dictionary<string, object>> but using a non specific string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM