How to get the value inside without knowing the json string key?

  1. how to get the value inside without knowing the json string key?

for example, something like the following:

let result=
    {
    "err": 0,
    "message": "",
    "data": {
    "fieldCount": 0,
        "affectedRows": 1,
        "insertId": 0,
        "serverStatus": 2,
        "warningCount": 0,
        "message": "(Rows matched: 1  Changed: 0  Warnings: 0",
        "protocol41": true,
        "changedRows": 0
    }
};
console.log(result.data.changedRows);//0

however, when I only know in the js program that result is a string of json, how to get the value in a similar situation.

  1. used the corresponding function to get it, but it was not successful.
let  text=JSON.stringify(result);
console.log(text.data.changedRows);
Jul.04,2022

    function getValue(obj){
        for(var i in obj){
            typeof obj[i]==='object' ? getValue(obj[i]) : console.log(obj[i])
        }
    }
    getValue(result)//val

I think, what if the key of json is a string? For example, "name"


can get an array of the values of an object through Object.values ().
all values can be obtained by recursion.


you can specify it directly using data.key.key after using let data = JSON.parse (JSONSting).

Menu