简体   繁体   English

将Visual Studio 2015 Web测试响应从十六进制更改为JSON

[英]Changing Visual Studio 2015 Web Test response from hex to JSON

How can the bottom pane of a Visual Studio Web Test response show JSON instead of hex values? Visual Studio Web测试响应的底部窗格如何显示JSON而不是十六进制值?

在此处输入图片说明

I have not found any way of getting just the json in the bottom panel. 我还没有找到在底部面板中仅获取json的任何方法。 Some responses include the "View in html browser" link so clicking that will normally show just the json. 一些响应包括“在html浏览器中查看”链接,因此单击通常将仅显示json。

The workaround I normally use is to copy the entire response body from the bottom panel, paste it into a text editor (you could have a text file open in Visual Studio as a work area, but I use Notepad++ for this job) and then remove the hex part of the copied text. 我通常使用的解决方法是从底部面板复制整个响应正文,将其粘贴到文本编辑器中(您可以在Visual Studio中将文本文件作为工作区打开,但我使用Notepad ++来完成此工作),然后删除复制文本的十六进制部分。 Both Visual Studio and Notepad++ support column (or box or rectangular) mode allowing the entire hex part to be selected and deleted. Visual StudioNotepad ++都支持列(或方框或矩形)模式,允许选择和删除整个十六进制部分。 The final action is to join the lines to for one long line. 最后的动作是将这些行连接成一条长行。 This job is so useful, but so tedious with an editor, that I wrote a little C# program to do it. 这项工作非常有用,但是对于编辑器却如此繁琐,以至于我编写了一个小C#程序来实现。

There is a way to do it. 有一种方法可以做到。 Create an extraction rule that selects a token. 创建选择令牌的提取规则。 This rule will always return positive so the token does not need to exist. 该规则将始终返回正值,因此令牌不需要存在。 When used, it will format JSON in the response window in JSON only: 使用时,它将仅在JSON的响应窗口中格式化JSON:

 namespace WebTestPlugins
{

[DisplayName("Output in JSON")]
[Description("Outputs Viewer in JSON")]
public class OutputInJSON : ExtractionRule
    {
     public override void Extract(object sender, ExtractionEventArgs e)
        {
        var response= e.Response.BodyString;
        var parseresponse = JObject.Parse(response);
        e.WebTest.Context.Add("xxxz", parseresponse.SelectToken("xxxx"));
        e.Success = true;
        return;
        }
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM