Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Angularjs1制作可行的JSON文件_Angularjs_Json - Fatal编程技术网

使用Angularjs1制作可行的JSON文件

使用Angularjs1制作可行的JSON文件,angularjs,json,Angularjs,Json,当我导航到本地主机服务器时,我有一个类似这样的文件 网址: 输出如下所示: "{\n \"firstName\" : \"Vladimir\"\n}\n" 现在,我想读这个文件,所以我用angular提供了服务: .service('messageService', ['$resource', function($resource){ this.getMessage = function(firstName) { var gmList = $resource("rea

当我导航到本地主机服务器时,我有一个类似这样的文件

网址:

输出如下所示:

"{\n  \"firstName\" : \"Vladimir\"\n}\n"
现在,我想读这个文件,所以我用angular提供了服务:

.service('messageService', ['$resource', function($resource){
    this.getMessage = function(firstName) {
        var gmList = $resource("read/lol.json");

        return gmList.get({
            firstName : firstName
        });
    };
}]);
还有我的controller.js

.controller('messageService', function(messageService){


  this.firstName = messageService.firstName;

  this.messageResult = messageService.getMessage(this.firstName);
});
最后是我的html文件

<div data-ng-controller="messageService as mService">
  <p>This is new controller: </p>
  <div >
    <ul data-ng-repeat="w in mService.messageResult">
      <li>{{w.firstName}}</li>
    </ul>

  </div>
就这样。我觉得需要做一些事情来忽略这些\n和。 当我加载我的页面时,我得到了一个空列表(点,列表中没有任何内容,只有点)。 然而,当我在控制台中查看时,我的lol.json文件的状态为200。我得到了那个文件,但不能从中得到任何东西(对象)


问题是什么?

使用JSON.parse。代码如下:

var data=JSON.parse(“{\n\“firstName\”:\“Vladimir\”\n}\n”
);

console.log(data.firstName)您是如何创建json文件的?它看起来不那么有效。应该是这样的:{“firstName”:“Vladimir”}要验证JSON,请使用:我编辑了下面的文章,看看它是如何在后端编写的。顺便说一下,JSON是有效的。当然,这是有效的。当转到data.firstName时,输出为“Vladimir”。如何摆脱“”?好吧,让我解释一下。当您只执行console.log(数据)时,您将只接收作为“对象”的日志,因此我们通常执行JSON.stringify(数据)以获取作为字符串的实际JSON。这就是为什么你能看到引号。检查我的更新代码,删除JSON.stringify,它满足您的需要,
<div data-ng-controller="messageService as mService">
  <p>This is new controller: </p>
  <div >
    <ul data-ng-repeat="w in mService.messageResult">
      <li>{{w.firstName}}</li>
    </ul>

  </div>
public Result getFileContent(String filename) throws IOException {

    String publicFolder = _appEnvironment.rootPath().getAbsolutePath().concat(folder);

    String result = "";

    FileReader in = null;
    BufferedReader br = null;

    File dataFile = new File(publicFolder + filename);

    try {
        in = new FileReader(dataFile);

        br = new BufferedReader(in);

        String line;
        while ((line = br.readLine()) != null) {
            result = result.concat(line).concat("\n");
        }
    } finally {
        if (in != null) {
            in.close();
        }
        if (br != null) {
            br.close();
        }
    }

    return ok(Json.toJson(result));
}