从文件加载JSON对象

时间:2023-01-24 09:50:38

I try to parse a JSON object (in Haxe) included in a file, but it's not working. When I try to parse a JSON object from a var in the class, it's working very well but that's not the case when the JSON is in a file, so I try to open the file and get the object inside.

我尝试解析文件中包含的JSON对象(在Haxe中),但它不起作用。当我尝试从类中的var解析JSON对象时,它工作得很好但是当JSON在文件中时并非如此,所以我尝试打开文件并将对象放入其中。

Thanks for help !

感谢帮助 !

3 个解决方案

#1


Parsing json is as simple as haxe.Json.parse(jsonvar). The problem you are experiencing is probably that you are using a target that doesn't support accessing files on the system (like JS for the browser or Flash for example). The solutions are multiple. If you are loading a configuration piece of data you can use a macro and do something like this:

解析json就像haxe.Json.parse(jsonvar)一样简单。您遇到的问题可能是您使用的目标不支持访问系统上的文件(例如浏览器的JS或Flash)。解决方案是多方面的。如果要加载配置数据,可以使用宏并执行以下操作:

class Config {
  // path is relative to where haxe is executed
  macro public static function json(path : String) {
    var value = sys.io.File.getContent(path),
        json = haxe.Json.parse(value);
    return macro $v{json};
  }
}

And you use in your code with:

并且您在代码中使用:

var conf = Config.json("myfile.json");

Note that in this scenario conf is initialized at compile time and there is no loading at runtime. That implies that if you make any changes to myfile.json you will not see them until you recompile.

请注意,在此场景中,conf在编译时初始化,并且在运行时没有加载。这意味着,如果对myfile.json进行任何更改,则在重新编译之前不会看到它们。

The other scenario is that you load your file at runtime. In that case the solution is often specific to your target (for JS in the browser you might want to make an AJAX call, for Neko or OpenFL you can load a file directly from the system similarly to what explained before).

另一种情况是您在运行时加载文件。在这种情况下,解决方案通常特定于您的目标(对于浏览器中的JS,您可能希望进行AJAX调用,对于Neko或OpenFL,您可以直接从系统加载文件,类似于之前解释的内容)。

#2


TJSON can take care of the JSON parsing: https://github.com/martamius/TJSON

TJSON可以处理JSON解析:https://github.com/martamius/TJSON

var myObject = TJSON.parse(sys.io.File.getContent('filename.json'));

#3


I recommend you post code in future - to at least add more context.

我建议您将来发布代码 - 至少添加更多上下文。

Have you used JSON parse to parse the strings you read in from the file?

您是否使用JSON解析来解析从文件中读取的字符串?

#1


Parsing json is as simple as haxe.Json.parse(jsonvar). The problem you are experiencing is probably that you are using a target that doesn't support accessing files on the system (like JS for the browser or Flash for example). The solutions are multiple. If you are loading a configuration piece of data you can use a macro and do something like this:

解析json就像haxe.Json.parse(jsonvar)一样简单。您遇到的问题可能是您使用的目标不支持访问系统上的文件(例如浏览器的JS或Flash)。解决方案是多方面的。如果要加载配置数据,可以使用宏并执行以下操作:

class Config {
  // path is relative to where haxe is executed
  macro public static function json(path : String) {
    var value = sys.io.File.getContent(path),
        json = haxe.Json.parse(value);
    return macro $v{json};
  }
}

And you use in your code with:

并且您在代码中使用:

var conf = Config.json("myfile.json");

Note that in this scenario conf is initialized at compile time and there is no loading at runtime. That implies that if you make any changes to myfile.json you will not see them until you recompile.

请注意,在此场景中,conf在编译时初始化,并且在运行时没有加载。这意味着,如果对myfile.json进行任何更改,则在重新编译之前不会看到它们。

The other scenario is that you load your file at runtime. In that case the solution is often specific to your target (for JS in the browser you might want to make an AJAX call, for Neko or OpenFL you can load a file directly from the system similarly to what explained before).

另一种情况是您在运行时加载文件。在这种情况下,解决方案通常特定于您的目标(对于浏览器中的JS,您可能希望进行AJAX调用,对于Neko或OpenFL,您可以直接从系统加载文件,类似于之前解释的内容)。

#2


TJSON can take care of the JSON parsing: https://github.com/martamius/TJSON

TJSON可以处理JSON解析:https://github.com/martamius/TJSON

var myObject = TJSON.parse(sys.io.File.getContent('filename.json'));

#3


I recommend you post code in future - to at least add more context.

我建议您将来发布代码 - 至少添加更多上下文。

Have you used JSON parse to parse the strings you read in from the file?

您是否使用JSON解析来解析从文件中读取的字符串?