替换字符串值中的值

时间:2017-05-20 04:29:06

标签: node.js

我正在尝试查看JSON响应并在每次“Sw”出现在字符串(值)中时替换。例如,获取此JSON对象:

[{"accountName":"MTVN\\lalalala",
  "baseOfficeLocation":"657 Hudson Street Floor 11",
  "department":"Mp - Engineering - Engineering",
  "jobTitle":"Sw Test Engineer",
  "preferredName":"Beddingfield,Natasha",
  "workEmail":"Natasha.Beddingfield@viacom.com"}]

每当我得到一个包含像“Mp”和“Sw”这样的屠杀词语的回复时,我想更换它们,并用它的全字替换它们。所以“Sw”将成为“软件”。

这是我到目前为止的一些代码,但我在node.js中根本没有经验,所以想知道如何实现这个(:

var baseOfficeLocation = jsonData.baseOfficeLocation;
            console.log("Base Office Location: " + baseOfficeLocation);
            var department = jsonData.department;
            console.log("Department: " + department);

            var workEmail = jsonData.workEmail;
            console.log("Work Email: " + workEmail);

            if (jsonData.jobTitle !== null) {
                replaceString(jsonData.jobTitle, 'Sw', 'Software');
            }
            console.log("Job Title: " + jobTitle);

2 个答案:

答案 0 :(得分:0)

$.ajax({
    url: wikiAPI,
    dataType: "json",
    success: displayResults //dont call the function, pass it. it will be called when the response is ready.
});

function displayResults(data){
  console.log(data);//here is your response
  $(".results").html("<p><h3>Hello World!</h3></p>");
}

答案 1 :(得分:0)

以下是替换字符串的解决方案:

var demo = [{"accountName":"MTVN\\lalalala",
    "baseOfficeLocation":"657 Hudson Street Floor 11",
    "department":"Mp - Engineering - Engineering",
    "jobTitle":"Sw Test Engineer",
    "preferredName":"Beddingfield,Natasha",
    "workEmail":"Natasha.Beddingfield@viacom.com"}]
for(var key in demo[0]) {
    demo[0][key] = demo[0][key].replace('Sw', 'Software');
}