首页 \ 问答 \ 以未定义的元素在JSON中循环(loop in JSON with undefined elements)

以未定义的元素在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
更新时间:2024-05-02 12:05

最满意答案

不,不是所有类型都有默认成员。

任何类模块都可以拥有一个默认成员,通过指定一个特殊的成员属性 (您必须手动导出和编辑代码文件来执行此操作,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 a Range object reference; Range.Cells is also a getter that returns a Range object; there is no Cell class in the Excel object model. The default member of a Range is a hidden [_Default] member that appears to resolve to its Value. 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 ...
  • 不,不是所有类型都有默认成员。 任何类模块都可以拥有一个默认成员,通过指定一个特殊的成员属性 (您必须手动导出和编辑代码文件来执行此操作,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 ...
  • 如果要保持实际数组隐藏且不可触及,除非通过方法,它必须在构造函数中声明。 任何改变它的函数也必须在构造函数中声明。 如果该项目可公开访问,则必须附加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 ...
  • 您需要声明该类的属性作为默认属性。 作为一个例子,下面是我写的一个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 ...
  • 区别在于如何通过Range和Sheets对象的实现来处理其默认属性的输入。 Range和Sheets对象的默认属性采用Variant类型的参数。 你可以传递任何东西,因此不需要强制类型。 在第一个示例中,您将Range对象传递给两者。 默认属性如何处理输入取决于它们自己。 显然, Range的属性尝试检索传递参数的默认值,在您的示例中,地址为String。 Sheets对象似乎不那么宽容并引发错误,因为您既没有传递数字也没有传递String。 不一致是VBA的优势之一...... 顺便说一句,传递CStr( ...

相关文章

更多

最新问答

更多
  • sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)
  • 如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)
  • AESGCM解密失败的MAC(AESGCM decryption failing with MAC)
  • Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)
  • 湖北京山哪里有修平板计算机的
  • SimplePie问题(SimplePie Problem)
  • 在不同的任务中,我们可以同时使用多少“上下文”?(How many 'context' we can use at a time simultaneously in different tasks?)
  • HTML / Javascript:从子目录启用文件夹访问(HTML/Javascript: Enabling folder access from a subdirectory)
  • 为什么我会收到链接错误?(Why do I get a linker error?)
  • 如何正确定义析构函数(How to properly define destructor)
  • 垂直切换菜单打开第3级父级。(Vertical toggle menu 3rd level parent stay opened. jQuery)
  • 类型不匹配 - JavaScript(Type mismatch - JavaScript)
  • 为什么当我将模型传递给我的.Net MVC 4控制器操作时,它坚持在部分更新中使用它?(Why is it that when I pass a Model to my .Net MVC 4 Controller Action it insists on using it in the Partial Update?)
  • 在使用熊猫和statsmodels时拉取变量名称(Pulling variable names when using pandas and statsmodels)
  • 如何开启mysql计划事件
  • 检查数组的总和是否大于最大数,反之亦然javascript(checking if sum of array is greater than max number and vice versa javascript)
  • 使用OpenGL ES绘制轮廓(Drawing Outline with OpenGL ES)
  • java日历格式(java Calendar format)
  • Python PANDAS:将pandas / numpy转换为dask数据框/数组(Python PANDAS: Converting from pandas/numpy to dask dataframe/array)
  • 如何搜索附加在elasticsearch索引中的文档的内容(How to search a content of a document attached in elasticsearch index)
  • LinQ to Entities:做相反的查询(LinQ to Entities: Doing the opposite query)
  • 从ExtJs 4.1商店中删除记录时会触发哪些事件(Which events get fired when a record is removed from ExtJs 4.1 store)
  • 运行javascript后如何截取网页截图[关闭](How to take screenshot of a webpage after running javascript [closed])
  • 如何使用GlassFish打印完整的堆栈跟踪?(How can I print the full stack trace with GlassFish?)
  • 如何获取某个exe应用程序的出站HTTP请求?(how to get the outbound HTTP request of a certain exe application?)
  • 嗨,Android重叠背景片段和膨胀异常(Hi, Android overlapping background fragment and inflate exception)
  • Assimp详细说明typedef(Assimp elaborated type refers to typedef)
  • 初始化继承类中不同对象的列表(initialize list of different objects in inherited class)
  • 使用jquery ajax在gridview行中保存星级评分(Save star rating in a gridview row using jquery ajax)
  • Geoxml3 groundOverlay zIndex(Geoxml3 groundOverlay zIndex)