代码之家  ›  专栏  ›  技术社区  ›  Bn.F76

为什么我不能用javascript打开()和parse()这个json文件?

  •  0
  • Bn.F76  · 技术社区  · 6 年前

    我使用django在python中构建一个Web serivce,我的任务之一是在我的项目中解析一个.json文件。

    代码会编译,但当我尝试访问JSON文件时,尝试保存数据的 var json_data

    <head>
    <meta charset=“utf-8”>
    <title>网络图3</title>
    <link rel=“stylesheet”href=“%static”css/style.css“%”>
    
    
    脚本& GT;
    //导入JSON;
    window.onload=函数()。{
    var ARR=[];
    var json_data=open(“%static”静态/json/graphdata.json“%”);
    
    var obj=json.parse(json_data);
    VARⅠ;
    console.log(json_数据)
    如果(obj==null){
    返回
    }
    对于(i=0;i<obj.documents;i++){
    点=目标文件[I];
    arr[point.id].y=分.score;
    }
    var chart=新canvasjs.chart(“chartcontainer”,){
    启用动画:真,
    主题:“Light2”,
    题目:{
    文本:“对话情感分析”
    }
    腋窝:{
    includezero:错误
    },请
    数据:[ {
    类型:“行”,
    数据点:ARR
    //
    //Y:450_
    //Y:414_
    //Y:520,indexLabel:“最高”,markercolor:“红色”,markerType:“三角形”,
    //Y:460_
    //Y:450_
    //Y:500_
    //Y:480_
    //Y:480_
    //Y:410,indexLabel:“最低”,markercolor:“深石板出口”,markertype:“交叉”,
    //Y:500_
    //Y:480_
    //Y:510_
    //]
    }
    (});
    图表。
    
    }
    & /脚本& GT;
    
    和/头& GT;
    < /代码> 
    
    

    示例JSON数据如下所示:

    “documents”:[“id”:“0”,“score”:0.8365770578384399,
    “id”:“2”,“分数”:0.989687561988306,
    “id”:“3”,“分数”:0.5,
    “id”:“4”,“分数”:0.5,
    “id”:“6”,“分数”:0.127222820043563843,
    “id”:“7”,“分数”:0.16494140028953552,
    “id”:“8”,“分数”:0.7551238536834717,
    “id”:“9”,“分数”:0.1290119401168823,
    “id”:“10”,“分数”:0.5,
    “id”:“11”,“分数”:0.7559014558792114,
    < /代码> 
    
    

    .var json_data当我试图访问JSON文件时,试图保存数据将变为空。

    <head>
     <meta charset="UTF-8">
     <title>Network Graph3</title>
     <link rel="stylesheet" href="{% static 'css/style.css' %}">
    
    
     <script>
      // import json;
      window.onload = function () {
       var arr = [];
       var json_data = open("{% static 'static/json/graphData.json' %}");
    
       var obj = JSON.parse(json_data);
       var i;
       console.log(json_data)
       if (obj == null){
         return
       }
       for (i = 0; i < obj.documents; i++){
         point = obj.documents[i];
         arr[point.id].y = point.score;
       }
       var chart = new CanvasJS.Chart("chartContainer", {
         animationEnabled: true,
         theme: "light2",
         title:{
             text: "Dialog Sentiment Analysis"
         },
         axisY:{
             includeZero: false
         },
         data: [{
             type: "line",
             dataPoints: arr
             // [
             //     { y: 450 },
             //     { y: 414},
             //     { y: 520, indexLabel: "highest",markerColor: "red", markerType: "triangle" },
             //     { y: 460 },
             //     { y: 450 },
             //     { y: 500 },
             //     { y: 480 },
             //     { y: 480 },
             //     { y: 410 , indexLabel: "lowest",markerColor: "DarkSlateGrey", markerType: "cross" },
             //     { y: 500 },
             //     { y: 480 },
             //     { y: 510 }
             // ]
         }]
       });
       chart.render();
    
     }
    </script>
    
    </head>
    

    示例JSON数据如下:

    {"documents": [{"id": "0", "score": 0.8365770578384399},
                {"id": "2", "score": 0.9896875619888306},
                {"id": "3", "score": 0.5},
                {"id": "4", "score": 0.5},
                {"id": "6", "score": 0.12722820043563843},
                {"id": "7", "score": 0.16494140028953552},
                {"id": "8", "score": 0.7551238536834717},
                {"id": "9", "score": 0.12901419401168823},
                {"id": "10", "score": 0.5},
                {"id": "11", "score": 0.7559014558792114},
    

    2 回复  |  直到 6 年前
        1
  •  0
  •   ewwink    6 年前

    在JavaScript中 open() 用于在新选项卡/窗口中打开URL而不读取内容,请使用 XMLHttpRequest() 或jQuery $.ajax() / .getJSON() . 或者你的意思是想做巨蟒 打开() ?

    javascript代码

    window.onload = function() {
      var xhr = new XMLHttpRequest();
      xhr.onreadystatechange = function() {
        if (xhr.readyState == XMLHttpRequest.DONE) {
          // proccess json here
          readJson(xhr.responseText);
        }
      }
      xhr.open('GET', "/'static/json/graphData.json=", true);
      xhr.send(null);
    }
    
    function readJson(json_data) {
      var arr = [];
      var obj = JSON.parse(json_data);
      var i;
      console.log(json_data)
      ....
      ....
    }
    
        2
  •  0
  •   felipsmartins Martin Zeltin    6 年前

    这里有很多问题…
    我假设您的代码需要调用 open() python函数,在这种情况下,不能从javascript上下文调用它,因为它将被计算为 window.open() (与python无关)而不是python函数。

    所以你所要做的就是从 view 并返回模板上下文作为序列化JSON字符串,如下所示:

    from django.shortcuts import render
    
    def my_view(request):
        context = {"data": None}
    
        with open('data.json') as f:
            context['data'] = json.load(f)
    
    
        return render(request, 'templates/my_template.html', context)
    

    现在就用吧 JSON.parse() . 另一个选择是依靠 $.getJSON() 如果您使用的是jquery或类似的库或ajax,则通过http从服务器获取json数据(它要求json文件具有通过http的公共访问权限)。