以未定义的元素在JSON中循环(loop in JSON with undefined elements)
我想弄清楚如何检索一个json的值,我不知道元素的数量。 例:
我的JSON可以是类似的
var json = ["fixelement1":"value1","fixelement2":"value2","fixelement3":"value3","variableelement4":"value4","variableelement5":"value5"]
要么
var json =["fixelement1":"value1","fixelement2":"value2","fixelement3":"value3","variableelement7":"value7","variableelement8":"value8", "variableelementN":"valueN"]
我唯一知道的是前3个元素总是相同的。 我使用.indexOf()来搜索fixelement3中的值。 我想要做的是,如果我找到元素,我想检索以下所有元素的名称(该数字是可变的,未知的)及其值。
javascript或jquery会为我工作,但我不知道..提前谢谢你!
I am trying to figure out how to retrieve the values of a json of which i do not know the number of elements. Example:
my json can be something like
var json = ["fixelement1":"value1","fixelement2":"value2","fixelement3":"value3","variableelement4":"value4","variableelement5":"value5"]
or
var json =["fixelement1":"value1","fixelement2":"value2","fixelement3":"value3","variableelement7":"value7","variableelement8":"value8", "variableelementN":"valueN"]
the only thing that I know is that the first 3 elements are always the same. I use .indexOf() to search a value in fixelement3. What I would like to do is, if I find the element, I would like to retrieve the name of all the following elements (which number is variable and that are unknown) and their values.
javascript or jquery would work for me, but I have no idea.. thank you in advance!
原文:https://stackoverflow.com/questions/41391482
最满意答案
不,不是所有类型都有默认成员。
任何类模块都可以拥有一个默认成员,通过指定一个特殊的成员属性 (您必须手动导出和编辑代码文件来执行此操作,VBE不会公开任何此功能):
Attribute {member name}.VB_UserMemId = 0
只有一个成员被允许成为类型的默认成员。
你正在发现默认会员的尴尬,以及为什么他们应该避免。
VBA做了很多事情来“让我们的生活更轻松”(例如隐式类型转换),而默认成员就是其中之一。
集合可以存储非静态引用
我不知道什么是“静态引用”,但是当你在一个
Collection
存储一个对象引用时,你并不是存储该对象的副本,而是存储它的引用 。
Cells()
和其他对象没有默认属性,如.value
。
Global.Cells
是一个返回Range
对象引用的参数化属性获取器;Range.Cells
也是一个返回Range
对象的getter; Excel对象模型中没有Cell
类。Range
的默认成员是一个隐藏的[_Default]
成员,似乎可以解析为其Value
。 但是当你这样做时:Dim c As Collection Set c = New Collection c.Add ActiveSheet.Cells(1, 1)
然后你添加
.Cells(1, 1)
返回的Range
引用,然后这个:Debug.Print c.Item(1)
将输出该
Range
对象的值。 然而这个:Debug.Print TypeName(c.Item(1))
将输出
Range
。混乱? 是。 这就是为什么你应该总是指定
Option Explicit
,尽可能使用明确类型声明的变量,避免隐式类型转换......并避免使用默认成员。通过编写完全按照其行为表示的代码,您可以避免大量的VBA陷阱,而当您最终想要学习一些VB.NET或C#时,您绝对不会遗漏类型安全性和显式性。
No, not all types have a default member.
Any class module can have a default member, by specifying a special member attribute (you'd have to export and edit the code file manually to do this, the VBE doesn't expose any functionality for this):
Attribute {member name}.VB_UserMemId = 0
Only one member is allowed to be a type's default member.
You're discovering the nastiness of default members and why they should be avoided.
VBA does a lot of things to "make our lives easier" (e.g. implicit type conversions), and default members is one of these things.
Collections can store non-static references
I don't know what a "static reference" is, but when you store an object reference in a
Collection
, you're not storing a copy of the object but a reference to it.
Cells()
and other objects do not have default properties such as.value
.
Global.Cells
is a parameterized property getter that returns aRange
object reference;Range.Cells
is also a getter that returns aRange
object; there is noCell
class in the Excel object model. The default member of aRange
is a hidden[_Default]
member that appears to resolve to itsValue
. But then when you do this:Dim c As Collection Set c = New Collection c.Add ActiveSheet.Cells(1, 1)
Then you're adding the
Range
reference that.Cells(1, 1)
returns, and then this:Debug.Print c.Item(1)
Will output that
Range
object's value. Yet this:Debug.Print TypeName(c.Item(1))
Will output
Range
.Confusing? Yes. That's why you should always have
Option Explicit
specified, work with variables declared with an explicit type as much as possible, avoid implicit type conversions, ...and avoid using default members.By writing code that reads exactly as it should behave, you avoid a number of VBA traps, and when you eventually want to learn some VB.NET or C# you won't be lost at all about type safety and explicitness.
相关问答
更多-
我看到的一件事是你没有正确构建你的表: //first row document.write("
"); for (var i = 0; i "); } 在这里你创建一行,然后创建一个单元格。 然后,当你得到表格行时,它是空的,这实际上是真的。 你需要的是这样的: document.write( ... -
你不需要Range 。 Cells()是一个范围对象: Dim rngTA As Range: Set rngTA = wsTA.Cells(24, ColNum) 当在Range使用范围对象时,它需要有两个开始和一个结束。 既然你只需要一个,就直接参考。 You do not need the Range. Cells() is a range object: Dim rngTA As Range: Set rngTA = wsTA.Cells(24, ColNum) When using range ...
-
cells()的默认属性(The default property for cells())[2022-05-20]
不,不是所有类型都有默认成员。 任何类模块都可以拥有一个默认成员,通过指定一个特殊的成员属性 (您必须手动导出和编辑代码文件来执行此操作,VBE不会公开任何此功能): Attribute {member name}.VB_UserMemId = 0 只有一个成员被允许成为类型的默认成员。 你正在发现默认会员的尴尬,以及为什么他们应该避免。 VBA做了很多事情来“让我们的生活更轻松”(例如隐式类型转换),而默认成员就是其中之一。 集合可以存储非静态引用 我不知道什么是“静态引用”,但是当你在一个Collec ... -
我不使用VB,但在这里它。 我发现使用控件的值 ,但它不是一个程序化的解决方案。 如果您有权访问该代码,请查找 Attribute Value.VB_UserMemId = 0 使用记事本。 Use OLE/Com Object Viewer, which is distributed with Microsoft Visual Studio. Go to type libraries and find the library the control is housed in, for example C ...
-
你应该可以通过创建你自己的TableCellEditor来完成这项工作,它可以使用setCellEditor()分配给父表。 该对象是一个工厂,当用户开始编辑单元格时,该工具由JTable调用,以创建用于执行实际编辑的字段。 你可以返回你自己的JTextField,并简单地避免设置旧值来实现你的要求。 您还必须将侦听器附加到文本字段,以在用户完成键入时更新表中的值。 You should be able to do this by creating your own TableCellEditor, whi ...
-
默认对象属性(Default Object Property)[2023-04-06]
如果要保持实际数组隐藏且不可触及,除非通过方法,它必须在构造函数中声明。 任何改变它的函数也必须在构造函数中声明。 如果该项目可公开访问,则必须附加this 。 class Post extends Array { add(val) { this.unshift(val); } remove() { this.shift(); } } class MyClass { constructor() { this.dat ... -
我能够通过SAS内核与SAS UE中的Jupyter合作。 所以我创建了一个单元格,type = Raw NBConvert,然后在单元格中选择了HTML。 然后我用SAS代码添加了一些测试单元。 然后我去了打印预览,顶部有一个按钮,显示代码节点折叠(实际上不可见)的输出,然后您可以按按钮让它们出现。 I was able to get it to work with Jupyter in SAS UE with the SAS kernel. So I created a cell, with type ...
-
您需要先激活工作表,然后才能在工作表上选择范围。 Option Explicit Sub SelectRange() 'What will be better is to reference the WorkBook as well. With Sheets("Working1") 'Activate the sheet before selecting the range .Activate 'You need to reference ...
-
未命名的默认属性(Unnamed Default Property)[2023-06-18]
您需要声明该类的属性作为默认属性。 作为一个例子,下面是我写的一个String包装类的一部分: class StringClass private finished_ private data_ private size_ public function init (val) finished_ = cStr(val) set init = me end function public default property ge ... -
返回默认属性(Returning the Default Property)[2023-11-23]
区别在于如何通过Range和Sheets对象的实现来处理其默认属性的输入。 Range和Sheets对象的默认属性采用Variant类型的参数。 你可以传递任何东西,因此不需要强制类型。 在第一个示例中,您将Range对象传递给两者。 默认属性如何处理输入取决于它们自己。 显然, Range的属性尝试检索传递参数的默认值,在您的示例中,地址为String。 Sheets对象似乎不那么宽容并引发错误,因为您既没有传递数字也没有传递String。 不一致是VBA的优势之一...... 顺便说一句,传递CStr( ...