作者在原文中说明了通过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#数组。