我可以使用Fiddler脚本修改JSON响应吗 [英] Can I Modify JSON responses using Fiddler Scripts

查看:272
本文介绍了我可以使用Fiddler脚本修改JSON响应吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我从服务器获得的响应(正文).我想修改一些键值对. 我使用了以下方法:

Below is the response(body) that I get from my server. I'm looking to modify some of the Key Value Pairs. I used the the following approach:

var bodystr=oSession.GetResponseBodyAsString();
var bodyjson=Fiddler.WebFormats.JSON.JsonDecode(bodystr); 

但是bodyjson没有我期望的任何内容. (我尝试使用MessageBox.Show(bodyjson.Sales.Qty);,但这会返回一个错误.)

But the bodyjson does not have any content that I expect. (I tried to use the MessageBox.Show(bodyjson.Sales.Qty); but this returns me an error.)

{
    "Sales" : {
        "Qty" : 1,
        "Item" : {
            "value" : "7"
        },
        "TaxCode" : {
            "value" : "NON"
        },
        "UnitPrice" : 3
    },
    "LineNum" : 0,
    "DetailType" : "Sales",
    "Amount" : 3,
    "Id" : "1"
}

除了字符串替换方法之外,是否还有其他方法可以更改JSON响应?

Is there any way, apart from string replace methods, to make changes to the JSON responses?

推荐答案

Fiddler的JsonDecode函数创建一个对象;它不会创建您期望的对象类型,即使它确实创建了,更改该对象中的值也不会对构成响应主体的字符串产生任何自动影响.

Fiddler's JsonDecode function creates an object; it doesn't create the type of object you expect and even if it did, changing values in that object wouldn't have any automatic impact on the string making up the response body.

请参见 http://www.telerik.com /forums/how-to-use-fiddler-webformats-json-jsondecode ,以了解该对象的工作原理.

See http://www.telerik.com/forums/how-to-use-fiddler-webformats-json-jsondecode for some insight into how this object works.

您需要执行bodyjson.JSONObject["Sales"]["Qty"]之类的操作来获取值.进行任何更改后,您需要在对象上调用JsonEncode以获得字符串,然后将响应的主体设置为该字符串.

You'd need to do something like bodyjson.JSONObject["Sales"]["Qty"] to get the value. After you make any changes, you'd need to call JsonEncode on the object to get a string and then set the response's body to that string.

    var s = '{"Sales" : {  "Qty" : 8,     "Item" : {            "value" : "7"          },          "TaxCode" : {            "value" : "NON"          },          "UnitPrice" : 3        },        "LineNum" : 0,        "DetailType" : "Sales",        "Amount" : 3,        "Id" : "1"}';
    var j = Fiddler.WebFormats.JSON.JsonDecode(s);
    MessageBox.Show(j.JSONObject["Sales"]["Qty"]);
    j.JSONObject["Sales"]["Qty"] = 4;
    MessageBox.Show(Fiddler.WebFormats.JSON.JsonEncode(j.JSONObject));

如果您只是想对正文进行微不足道的更改,则根本不用将字符串变成一个对象,只需直接更改字符串本身即可.

If you simply want to make a trivial change to the body text, don't bother turning the string into an object at all, simply change the string itself directly.

这篇关于我可以使用Fiddler脚本修改JSON响应吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆