-1

Just recently started learning Javascript and read alot about JSON and its benefits.I am doing my own little project and would like to receive help. I want to take JSON values into javascript code but it's not working. I have tried parsing it with this : var obj = JSON.parse(txt); but that didn't work. Below is my code that better demonstrates my problem.

<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}';
Swink
  • 353
  • 3
  • 26
  • 1
    So, where is the actual JSON values you want to parse, and where is JSON.parse? – Blazes Nov 28 '18 at 22:39
  • @Blazes i mean in json i have for example 'Simba' and its value is 95, i want that into javascript , where i have mentioned it, i tried json.parsing what couldnt get it to work took it away, with the help of w3school –  Nov 28 '18 at 22:42
  • 1
    @joonas Can you please edit your snippet so we can have a working example of what are you trying to achieve? Also, what's the text you want to write so that it will be later parsed as a JSON? – Adrian Pop Nov 28 '18 at 22:46
  • does your JSON begin with values=? If so it's not a valid JSON format... – soulpaul Nov 28 '18 at 22:47
  • in the json: "Simba" : 95(this is value which i want to take into javascript) –  Nov 28 '18 at 22:48
  • If you want to take a text from an input and later parse it, you have to provide a full, correct formated JSON. Basically you'd have to insert the string `"{"Kalle" : 75, "Pekka" : 59, "Jussi" : 8, "Simba" : 95}"` and it will work. If you want to insert just a name and a value and add them to an internal dictionary, is a bit different. However, you'll get an answer faster if the fiddle you provided would work as expected. – Adrian Pop Nov 28 '18 at 22:51
  • Please [edit] your question, click the edit the above snippet link, and put the HTML in the box labeled HTML, and your JavaScript into the box labeled JavaScript. Be sure to move the script that's in your HTML into the JavaScript box appropriately. – Heretic Monkey Nov 28 '18 at 22:53
  • Maybe this helps? [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/q/11922383/218196) *"that didn't work"* isn't really a useful problem description. – Felix Kling Nov 28 '18 at 22:55
  • @Adi one kind guy just answered correctly but deleted answer afterwards ... –  Nov 28 '18 at 23:00

1 Answers1

1

You need to parse the values string into JSON before you can access the properties.

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
    });

  });

Or something like:

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
    });

  });
mhatch
  • 4,441
  • 6
  • 36
  • 62
  • Thank you for the right answer, could you also do the same thing without JSON.parse();? –  Nov 29 '18 at 15:36