作者在原文中說明了通過C#調用Javascript來解析Json字符串的方法。代碼如下:
復制代碼 代碼如下:
using Microsoft.JScript;
using Microsoft.Vsa;
using Microsoft.JScript.Vsa;
public static Dictionary<string, string> GetMyValue(string jsonSource, string key1, string key2)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
string jsonStr = "function handleJson() {var j=" + jsonSource + ";var arr1=new Array();var arr2=new Array();for(var " + key2 + " in j." + key1 + ") arr1.push(" + key2 + "); return arr1;} handleJson(); ";
object o = Eval.JScriptEvaluate(jsonStr, VsaEngine.CreateEngine());
ArrayObject arr = (ArrayObject)o;
for (var i = 0; i <= (int)arr.length - 1; i++)
{
dic.Add(arr[i].ToString(), arr[i].ToString());
}
return dic;
}
大致就是這個樣子,通過C#調用js,用Js解析Json,並將返回值轉換為C#數組。