Copy link to clipboard
Copied
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'));
}
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
Copy link to clipboard
Copied
Come on, all kinds of strange and absurd attempts.
No, I need to change my thinking.
myJSON.{[n\} = r;
myJSON.[(n)] = r;
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
If the following line cause an error for you
myJSON.n = v;
Try the following
myJSON[n] = v;
-Manan
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
You are an invincible god.
Success!
Thank you very much.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now