【问题标题】:Json values into javascript [duplicate]将 Json 值转换为 javascript [重复]
【发布时间】:2019-04-30 22:18:41
【问题描述】:

最近刚开始学习 Javascript,阅读了很多关于 JSON 及其好处的文章。我正在做自己的小项目,希望得到帮助。我想将 JSON 值带入 javascript 代码,但它不起作用。我试过用这个来解析它:var obj = JSON.parse(txt); 但这没有用。下面是我的代码,可以更好地说明我的问题。

<body >
<h1>  person2</h1>
 <div class="koko">
     <div id="hh1" class="oee"></div>
 
    <div id="hh2" class="gauge" data-value="  // here the value of json  "></div><br>
	<div id="gg3" class="gauge"></div><br>
    <div id="hh4" class="gauge"></div>
  </div>  

  
  <script src="raphael-2.1.4.min.js"></script>
  <script src="justgage.js"></script>
  <script>
  document.addEventListener("DOMContentLoaded", function(event) {

    var dflt = {
      min: 0,
      max: 100,
   //   donut: true,
      gaugeWidthScale: 1.1,
      counter: true,
      hideInnerShadow: true
    }

    var hh1 = new JustGage({
      id: 'hh1',
      value:   , // here the value of json
      title: 'Kalle ',
      defaults: dflt
    });

    var hh2 = new JustGage({
      id: 'hh2',
      title: 'Pekka',
      defaults: dflt
    });
	
	    var hh3 = new JustGage({
      id: 'hh3',
      value:  , // here the value of json
      title: 'Jussi',
      defaults: dflt
    });
	
	    var hh4 = new JustGage({
      id: 'hh4',
      value:   , // here the value of json for Simba
      title: 'Simba',
      defaults: dflt
    });

  });
  
  </script>
</body>
values= '{"Kalle" : 75, "Pekka" : 59, "Jussi" : 8, "Simba" : 95}';

【问题讨论】:

  • 那么,您要解析的实际 JSON 值在哪里,JSON.parse 在哪里?
  • @Blazes 我的意思是在 json 中我有例如 'Simba' 并且它的值是 95,我想把它放到 javascript 中,我已经提到过它,我尝试了 json.parsing 什么无法让它工作在 w3school 的帮助下把它拿走了
  • @joonas 你能编辑你的sn-p,这样我们就可以有一个你想要实现的工作示例吗?另外,您想写什么文本以便稍后将其解析为 JSON?
  • 您的 JSON 是否以 values= 开头?如果是这样,它不是有效的 JSON 格式...
  • 在 json 中:“Simba”:95(这是我想带入 javascript 的值)

标签: javascript mysql json html


【解决方案1】:

您需要先将values 字符串解析为JSON,然后才能访问属性。

var hh1 = new JustGage({
      id: 'hh1',
      value:   (JSON.parse(values)).Kalle, // here the value of json
      title: 'Kalle ',
      defaults: dflt
    });

    var hh2 = new JustGage({
      id: 'hh2',
      title: 'Pekka',
      defaults: dflt
    });

        var hh3 = new JustGage({
      id: 'hh3',
      value:  (JSON.parse(values)).Jussi, // here the value of json
      title: 'Jussi',
      defaults: dflt
    });

        var hh4 = new JustGage({
      id: 'hh4',
      value:   (JSON.parse(values)).Simba, // here the value of json for Simba
      title: 'Simba',
      defaults: dflt
    });

  });

或者类似的东西:

values = JSON.parse(values);

var hh1 = new JustGage({
      id: 'hh1',
      value:   values.Kalle, // here the value of json
      title: 'Kalle ',
      defaults: dflt
    });

    var hh2 = new JustGage({
      id: 'hh2',
      title: 'Pekka',
      defaults: dflt
    });

        var hh3 = new JustGage({
      id: 'hh3',
      value:  values.Jussi, // here the value of json
      title: 'Jussi',
      defaults: dflt
    });

        var hh4 = new JustGage({
      id: 'hh4',
      value:   values.Simba, // here the value of json for Simba
      title: 'Simba',
      defaults: dflt
    });

  });

【讨论】:

  • 谢谢你的正确答案,如果没有 JSON.parse(); 你也能做同样的事情吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-26
  • 2021-11-01
  • 2019-04-10
  • 2020-02-06
  • 2015-01-02
  • 2015-04-21
  • 1970-01-01
相关资源
最近更新 更多