小编rr7*_*789的帖子

Excel VBA:解析的JSON对象循环

下面的示例...从解析的JSON字符串循环对象返回错误"对象不支持此属性或方法".任何人都可以建议如何使这项工作?非常感谢(我在这里问了6个小时寻找答案).

用于将JSON字符串解析为对象的函数(这可以正常工作).

Function jsonDecode(jsonString As Variant)
    Set sc = CreateObject("ScriptControl"): sc.Language = "JScript" 
    Set jsonDecode = sc.Eval("(" + jsonString + ")")
End Function
Run Code Online (Sandbox Code Playgroud)

循环解析对象将返回错误"对象不支持此属性或方法".

Sub TestJsonParsing()
    Dim arr As Object 'Parse the json array into here
    Dim jsonString As String

    'This works fine
    jsonString = "{'key1':'value1','key2':'value2'}"
    Set arr = jsonDecode(jsonString)
    MsgBox arr.key1 'Works (as long as I know the key name)

    'But this loop doesn't work - what am I doing wrong?
    For Each keyName In arr.keys 'Excel errors out …
Run Code Online (Sandbox Code Playgroud)

excel vba json scriptcontrol

21
推荐指数
2
解决办法
5万
查看次数

DataTable:使用LINQ With Criteria字段获取最大值(GroupBy)

我有一个像这样结构的DataTable:

用户名| 价钱

"杰克".01

"杰克".02

"玛丽".03

"玛丽".04

如何在DataTable上使用LINQ返回MAX PRICE FOR JACK(例如:.02)?

设置表的代码(可以忽略).

DataTable tbl = new DataTable();
tbl.Columns.Add("username");
tbl.Columns["username"].DataType = typeof(string);

tbl.Columns.Add("price");
tbl.Columns["price"].DataType = typeof(double);

DataRow r = tbl.NewRow();
r["username"] = "jack"; r["price"] = .01;
tbl.Rows.Add(r);

r = tbl.NewRow();
r["username"] = "jack"; r["price"] = .02;
tbl.Rows.Add(r);

r = tbl.NewRow();
r["username"] = "mary"; r["price"] = .03;
tbl.Rows.Add(r);

r = tbl.NewRow();
r["username"] = "mary"; r["price"] = .04;
tbl.Rows.Add(r);
Run Code Online (Sandbox Code Playgroud)

这是我被卡住的地方.想以最高价格为杰克退回一行(例如:".02").

var result =
    from row in tbl.AsEnumerable() 
    where (string) row["username"] == "jack"
    group …
Run Code Online (Sandbox Code Playgroud)

c# linq datatable

4
推荐指数
2
解决办法
4万
查看次数

标签 统计

c# ×1

datatable ×1

excel ×1

json ×1

linq ×1

scriptcontrol ×1

vba ×1