[英]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.