简体   繁体   中英

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#). The json object like below:

[{
  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:

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. For example, how can I modify the child object with the 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. So my question is how to modify the object in List>. Thanks.

ps: I also want to know after edit, how to convert the object to json string. Thanks.

ps: On client side, the function below build the object:

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:

  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:

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

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. 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. 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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM