Skip to main content
dublove
Legend
August 6, 2025
解決済み

How to transfer myparStn to n in myJSON.n = v;, changing it to myJSON.myparStn = v;

  • August 6, 2025
  • 返信数 3.
  • 322 ビュー

I want to reuse the rwJSON() function, but I don't know how to pass the name to "myJSON. n = v";
For example, I want to write data with a field (name is myParStn, value is 20) to my.json, but I found that rwJSON(myPar, v) cannot be implemented.

 

Here, n cannot be used as a parameter.
It can be used as a field name on its own.

 

rwJSON(myparStn,r);
function rwJSON(n,v) {
    // read the json file's contents
    var myJSON = readFile(pathToMyJSONFile);
    if (!myJSON)
        return alert('Could not read JSON file.');
    // parse as json
    myJSON = JSON.parse(myJSON);
    // input text;
    myJSON.n = v;   
    var f = writeFile(pathToMyJSONFile, JSON.stringify(myJSON, undefined, '\t'));
}

 

解決に役立った回答 Manan Joshi

Try the following

var abc="111";
rwJSON("myparStn",abc);

function rwJSON(n,v) {
    // read the json file's contents
    var myJSON = readFile(pathToMyJSONFile);
    if (!myJSON)
        return alert('Could not read JSON file.');
    // parse as json
    myJSON = JSON.parse(myJSON);
    // input text;
    //myJSON.n = v;   
     myJSON[n] = v;  
    var f = writeFile(pathToMyJSONFile, JSON.stringify(myJSON, undefined, '\t'));
}

-Manan

返信数 3

Community Expert
August 7, 2025

If the following line cause an error for you

myJSON.n = v; 

Try the following 

myJSON[n] = v;

 -Manan

-Manan
dublove
dublove作成者
Legend
August 7, 2025

Hi @Manan Joshi 

Sorry for not responding in a timely manner。

I seem to see hope. But something is not right.

The key to the problem becomes how to read field names from JSON files (such as myParStn)


My target JSON is like this (for example: aa.json)

{
"myParStn": "222",
"myChaStn": "HH"
}

I followed your advice and made the following modifications:

var abc="111";
rwJSON(myparStn,abc);

function rwJSON(n,v) {
    // read the json file's contents
    var myJSON = readFile(pathToMyJSONFile);
    if (!myJSON)
        return alert('Could not read JSON file.');
    // parse as json
    myJSON = JSON.parse(myJSON);
    // input text;
    //myJSON.n = v;   
     myJSON[n] = v;  
    var f = writeFile(pathToMyJSONFile, JSON.stringify(myJSON, undefined, '\t'));
}

 The result indicates that myParStn is undefined,

 

So I guess, first we need to read myParStn from aa.json.
So I added in front of it:

var abc="111";
var myJSON = readFile(pathToMyJSONFile);
if (!myJSON)
return alert('Could not read JSON file.');
// parse as json
myJSON = JSON.parse(myJSON);
rwJSONw(myJSON.myParStn,abc);

The result of aajson is as follows:

{
"myParStn": "222",
"myChaStn": "HH",
"222": "111"
}

Obviously, I still haven't obtained the myParStn field itself.
I just got the original value of myParStn

 

 

Manan JoshiCommunity Expert解決!
Community Expert
August 8, 2025

Try the following

var abc="111";
rwJSON("myparStn",abc);

function rwJSON(n,v) {
    // read the json file's contents
    var myJSON = readFile(pathToMyJSONFile);
    if (!myJSON)
        return alert('Could not read JSON file.');
    // parse as json
    myJSON = JSON.parse(myJSON);
    // input text;
    //myJSON.n = v;   
     myJSON[n] = v;  
    var f = writeFile(pathToMyJSONFile, JSON.stringify(myJSON, undefined, '\t'));
}

-Manan

-Manan
Community Expert
August 7, 2025

Give the JSON within which you want to write and then we shall try. Just looking at the code is one aspect we don't know what structure your JSON has and what you are trying to add into it

-Manan

-Manan
dublove
dublove作成者
Legend
August 6, 2025

Come on, all kinds of strange and absurd attempts.

No, I need to change my thinking.

 myJSON.{[n\} = r;
  myJSON.[(n)] = r;