A
A
Anton2021-08-22 04:51:33
JavaScript
Anton, 2021-08-22 04:51:33

How to change such a JSON parser?

Greetings, please help me how to change such a parser to JSON for a new server response.

Old server response:

{"names":[{"name":"Oleg","act":0,"total":0},{"name":"Vitya","act":2,"total":2}]}

Old parser:
names = appData.filter( function(x) { return skipped.indexOf(x) < 0; })
        get("https://serv.io/set?" + names.join("|")).then(function(data) {
          result = JSON.parse(data)["names"]
          for (var i = 0; i < result.length; i++) {
            name = result[i]["name"]
            act = result[i]["act"]
            total = result[i]["total"]
          }

New server response:
{"Oleg":{"act":0,"total":0},"Vitya":{"act":2,"total":2}}

As you can see in the new answer, as I understand it, there is no array and here the name is the name of the object.
The question is how to change the old parser for the new server response.
I will be grateful for your help!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2021-08-22
@boypush

names = appData.filter(function (x) { return skipped.indexOf(x) < 0; })
get("https://serv.io/set?" + names.join("|")).then(function (data) {
    result = JSON.parse(data);
    
    Object.entries(result).forEach(([name,{act, total}])=>{
        console.log(name,act,total) // переменные name,act,total тут локальны, но сами уже разберетесь
    });

here is an example on jsfiddle https://jsfiddle.net/tfxe4q51/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question