-1

I am trying to convert list of string into json object. My string is : personName + rank. This is string is arranged as per the person's rank. PS: the backend code is in solidity My code so far is as follows:

    var ranks = new Array(5);
    PersonNames = Object.keys(names);
    ranks = contractInstance.dashboard.call(PersonNames);// function in solidity

    var finalList = [];
    for (var j = 0; j < ranks.length; j++){
      var winList = web3.toAscii(ranks[j]);
      var winCount = contractInstance.RanksFor.call(ranks[j]).toString();
      var person = {};
        person["name"] = winList[j];
        console.log(winList[j]);
        person["ranks"] = winCount[j];
      finalList.push(person);
    }
var myJSON = JSON.stringify(finalList);

Please let me know if I am making use of the correct syntax. I am learning how to implement javascript. Any help would be appreciated. Thank you!!

aib92
  • 23
  • 1
  • 6
  • https://stackoverflow.com/questions/45015/safely-turning-a-json-string-into-an-object duplicate. Additionally why aren't you just building a JSON object using the function. Instead of turning it to and from a json object? – Francisco Aug 01 '18 at 20:11
  • Question is Confusing. Are you creating a JSON String? Your code is creating a JSON String. What is the objective? – Daredevil Aug 01 '18 at 20:14
  • My output is coming undefined – aib92 Aug 01 '18 at 20:30
  • My winList holds name of people who received highest rank. and wincount holds their rank number. My final objective is to generate table which contains person's name and his/her rank. – aib92 Aug 01 '18 at 20:32

1 Answers1

0

I think using the provided method is the best way...

Example 1:

var json = JSON.parse('{ "hello":"world" }');

Example 2:

var json = '{"hello":"world"}';
obj = JSON.parse(json);
moo cow
  • 181
  • 13