Comments (5)
@daverave1212 The return type of haxe.Json.parse
is Dynamic
. To iterate through the keys, you are looking for a Map
-like interface to Dynamic
, which is possible with haxe.DynamicAccess
. It is an abstract over Dynamic
, so you can simply assign the return value of parse
to a variable with type DynamicAccess
:
var myObj:haxe.DynamicAccess<Dynamic> = haxe.Json.parse('{"foo": true, "bar": 2}');
for (key in myObj.keys()) trace(key, myObj.get(key));
// or using Haxe 4 syntax:
for (key => value in myObj) trace(key, value);
If you are sure that all the values in a given object will have the same type, you can use DynamicAccess
with the given type as type parameter:
var myObj:haxe.DynamicAccess<Int> = haxe.Json.parse('{"foo": 1, "bar": 2}');
for (key => value in myObj) {
// here, `value` is `Int`, rather than `Dynamic` as in the previous example
trace(key, value);
}
from haxe.org-comments.
Is there a way to parse maps from JSON into actual maps?
I can iterate through an array or even push into an array.
But if I try to iterate through a map, I'm getting a TypeError.
An example: https://try.haxe.org/#08f25
from haxe.org-comments.
@KaiSD I'd recommend using a library like json2object or tink_json for that.
from haxe.org-comments.
@KaiSD thats not an actual map, its just a plain object. You could iterate that using Reflect.
for (k in Reflect.fields(o.map)) {
trace(k, Reflect.field(o.map, k));
}
.. or type it as haxe.DynamicAccess
like @Aurel300 just describe above.
from haxe.org-comments.
How do you iterate through the keys of a parsed JSON object?
from haxe.org-comments.
Related Issues (20)
- [haxe.org/manual] Target Details HOT 1
- [haxe.org/manual] Generic HOT 2
- [haxe.org/manual] Null Safety HOT 2
- [haxe.org/manual] Blocks HOT 3
- [haxe.org/manual] Tail Recursion Elimination (TRE) HOT 1
- [haxe.org/manual] Module Sub-Types HOT 3
- [code.haxe.org] Macros - Working with compiler flags HOT 1
- [haxe.org/manual] Extractors HOT 2
- [haxe.org/manual] Array Access (Types - Abstract) HOT 2
- [haxe.org/manual] Overriding Methods HOT 1
- [haxe.org/manual] Variable capture HOT 2
- [haxe.org/manual] Tracing Types HOT 1
- [code.haxe.org] Beginner - Using numbers HOT 2
- [haxe.org/manual] Defines (Target Details - C++) HOT 7
- [haxe.org/manual] Import defaults / import.hx HOT 1
- [haxe.org/manual] Type Parameters (Type System) HOT 10
- [haxe.org/manual] Access Control HOT 1
- [haxe.org/manual] Inline (Class Fields - Access Modifier) HOT 1
- [haxe.org/manual] Encoding HOT 2
- [haxe.org/manual] Constraints HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from haxe.org-comments.