我已成功拦截IIS7 / ASP.NET应用程序中的HTTP Web请求并使用HttpModule我检测到内容类型为“application / json charset = UTF-8”。
我使用Proxy tool来确定JSON内容的样子。
我正在使用HttpModule拦截此Web请求,我想修改JSON内容。
我的问题是:如何将JSON内容提取到JSON对象中,修改它,并使用我的更改更新原始Web请求?
例如,假设JSON内容类似于
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
那我想做的是:
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "C H A N G E 1",
"SortAs": "C H A N G E 2",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["AAA", "BBB", "CCC"]
},
"GlossSee": "markup"
}
}
}
}
}
我正在使用C#代码来实现HttpModule。
答案 0 :(得分:2)
使用Newtonsoft.Json可以执行此操作:
dynamic json = JObject.Parse("{'glossary':{'title':'example glossary','GlossDiv':{'title':'S','GlossList':{'GlossEntry':{'ID':'C H A N G E 1','SortAs':'C H A N G E 2','GlossTerm':'Standard Generalized Markup Language','Acronym':'SGML','Abbrev':'ISO 8879:1986','GlossDef':{'para':'A meta-markup language, used to create markup languages such as DocBook.','GlossSeeAlso':['AAA','BBB','CCC']},'GlossSee':'markup'}}}}}");
json.glossary.GlossDiv.GlossList.GlossEntry.ID = 1234;
json.glossary.GlossDiv.GlossList.GlossEntry.SortAs = "abcde";
string result = json.ToString();