Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Apigee中使用JavaScript在多个位置替换json属性值_Javascript_Apigee - Fatal编程技术网

如何在Apigee中使用JavaScript在多个位置替换json属性值

如何在Apigee中使用JavaScript在多个位置替换json属性值,javascript,apigee,Javascript,Apigee,我需要使用JavaScript替换json请求中多次出现的属性值。我曾在JSFIDLE中尝试过这一点,但在Apigee JavaScript策略中的相同代码并没有替换该值 我有如下json数据: [ { "Name": "app1", "groups": [ { "desc": "this is a test group", "id": "test1",

我需要使用JavaScript替换json请求中多次出现的属性值。我曾在JSFIDLE中尝试过这一点,但在Apigee JavaScript策略中的相同代码并没有替换该值

我有如下json数据:

[
    {
        "Name": "app1",
        "groups": [
            {
                "desc": "this is a test group",
                "id": "test1",
                "name": "test grp45"
            },
            {
                "desc": "this is another test group",
                "id": "test2",
                "name": "test group 2"
            }
        ],
        "id": "1"
    },
    {
        "Name": "app2",
        "groups": [
            {
                "desc": "this is a test group",
                "id": "test3",
                "name": "test group 4"
            },
            {
                "desc": "this is another test group",
                "id": "test4",
                "name": "test group 4"
            }
        ],
        "id": "2"
    },
    {
        "Name": "app3",
        "groups": [
            {
                "desc": "this is a test group",
                "id": "test5",
                "name": "test group 5"
            },
            {
                "desc": "this is another test group",
                "id": "test6",
                "name": "test group 6"
            }
        ],
        "id": "3"
    }
]
以下是我尝试过的:

var val = context.getVariabl("request.content");
context.setVariable("val", val);

function findAndReplace(val1, value, replacevalue) {
    for (var x in val1) {
        if (typeof val1[x] == typeof {}) {
            findAndReplace(val1[x], value, replacevalue);
        }
        if (val1[x] == value) {
            val1["name"] = replacevalue;
            //break; // uncomment to stop after first replacement
        }
    }
}
findAndReplace(val, "test1", "img");
var result = JSON.stringify(val);
var obj = JSON.parse(result);
context.setVariable("response.content", obj);    

我想将“test1”的值替换为“img”。

首先,您要将
response.content
设置为解析的
obj
。你会想要:

context.setVariable("response.content", result);
…相反,因为流变量需要是字符串,而不是JavaScript对象

其次,您将获得
request.content
,然后设置
response.content
。您可能只需要一个或另一个,特别是考虑到此策略可能附加到请求流或响应流,而不是两者(您不能在响应流中设置请求,并且
内容。响应
将被目标响应覆盖)


使用Apigee跟踪工具查看策略在流中执行的位置,并检查设置的变量——这将帮助您确定需要修复的内容

正如上面所建议的,要想让它发挥作用,你需要做几件事

首先,我建议您使用两种策略。上面的javascript示例将在请求流中调用-上面的示例将对象的字符串化版本存储到中间流变量(如“foo”)中,如下所示:

var val = JSON.parse(context.getVariable("request.content"));
findAndReplace(val, "test1", "img");
context.setVariable("foo",JSON.stringify(val));
在响应流中,您可以使用分配消息策略在响应中插入“foo”。详情如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="assignFoo">
    <DisplayName>assignFoo</DisplayName>
    <FaultRules/>
    <Properties/>
    <Set>
        <Headers/>
        <Payload contentType="application/json; charset=utf-8">{foo}</Payload>
        <StatusCode>200</StatusCode>
        <ReasonPhrase>OK</ReasonPhrase>
    </Set>
    <AssignTo createNew="false" transport="http" type="response" />
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</AssignMessage>

让福
{foo}
200
好啊
真的
使用字符串

这会将JSON转换为字符串

JSON.stringify(json).replace("test1", "img")
然后你可以用替换的值将它转换回JSON

JSON.parse(json)