如何在json中更改特定嵌套数组的值

fnatzsnv  于 2022-12-15  发布在  其他
关注(0)|答案(1)|浏览(128)

我有下面的.json文件,我只想根据用户输入更改down和up值。

"android": {
    "appium:autoAcceptAlerts": true,
    "appium:automationName": "UiAutomator2",
    "appium:newCommandTimeout": 180,
    "appium:appPackage": "com.android.settings",
    "appium:platformName": "android",
    "appium:capture.networkConfig": {
      "shaping": {
          "down":5,
          "up": 2
      }
      }
},

上面的json主体我尝试使用下面的代码将“down”属性从“5”更改为“2”,将“up”属性从“2”更改为“1”。

if(state == 'high')
    {
        let tempVal = caps[platform];  // let platform = 'android'
        console.log("**********************");
        console.log(tempVal);  // It's printing the json body.
    
        for(let prep in tempVal)
        {
          tempVal[prep].shaping.down = 2;   //Not replaced.
          tempVa[prep].shaping.up = 1;    //Not replaced.

          console.log(tempVal[prep].shaping.down); // It's not printing down and returned as undefined.
        }
        console.log("**********************")
    }
btqmn9zl

btqmn9zl1#

if(state == 'high')
{
    let tempVal = caps[platform];  // let platform = 'android'
    console.log("**********************");
    console.log(tempVal);  // It's printing the json body.
let prep = 'appium:capture.networkConfig';
   
      tempVal[prep].shaping.down = 2;   
      tempVa[prep].shaping.up = 1;    

      console.log(tempVal[prep].shaping.down);  
    
    console.log("**********************")
}

相关问题