Code Monkey home page Code Monkey logo

Comments (5)

Aurel300 avatar Aurel300 commented on June 20, 2024 1

@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.

KaiSD avatar KaiSD commented on June 20, 2024 1

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.

Gama11 avatar Gama11 commented on June 20, 2024 1

@KaiSD I'd recommend using a library like json2object or tink_json for that.

from haxe.org-comments.

markknol avatar markknol commented on June 20, 2024 1

@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.

daverave1212 avatar daverave1212 commented on June 20, 2024

How do you iterate through the keys of a parsed JSON object?

from haxe.org-comments.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.