分享一个Json定位的fiddler插件源码
本帖最后由 sgbyg 于 2024-8-17 08:51 编辑using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Fiddler;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public class JsonPathClickLogger : IAutoTamper
{
private TabPage tabPage;
private RichTextBox richTextBox;
private Dictionary<int, string> positionToPathMap = new Dictionary<int, string>();
public void AutoTamperRequestAfter(Session oSession) { }
public void AutoTamperRequestBefore(Session oSession) { }
public void AutoTamperResponseAfter(Session oSession) { }
public void AutoTamperResponseBefore(Session oSession) { }
public void OnBeforeUnload() { }
public void OnBeforeReturningError(Session oSession) { }
public void OnLoad()
{
tabPage = new TabPage("生成");
richTextBox = new RichTextBox();
richTextBox.Dock = DockStyle.Fill;
richTextBox.Font = new Font("Consolas", 10);
richTextBox.ReadOnly = true;
richTextBox.MouseClick += new MouseEventHandler(RichTextBox_MouseClick);
tabPage.Controls.Add(richTextBox);
FiddlerApplication.UI.tabsViews.TabPages.Add(tabPage);
FiddlerApplication.AfterSessionComplete += delegate(Session session)
{
if (session.oResponse.MIMEType.Contains("application/json"))
{
string json = session.GetResponseBodyAsString();
if (IsJson(json))
{
string prettyJson = PrettyPrintJson(json);
FiddlerApplication.UI.Invoke((MethodInvoker)delegate
{
richTextBox.Text = prettyJson;
MapJsonPositions(prettyJson);
});
}
}
};
}
private void RichTextBox_MouseClick(object sender, MouseEventArgs e)
{
int index = newTxt.GetCharIndexFromPosition(e.Location);
string jsonPath;
if (positionToPathMap.TryGetValue(index, out jsonPath))
{
FiddlerApplication.UI.Text = jsonPath + " ---> 已复制到剪贴板";
Clipboard.SetText(jsonPath);
}
else
{
FiddlerApplication.UI.Text = "请点击在Json的key上";
}
}
private bool IsJson(string input)
{
input = input.Trim();
return (input.StartsWith("{") && input.EndsWith("}")) || (input.StartsWith("[") && input.EndsWith("]"));
}
private string PrettyPrintJson(string json)
{
try
{
JObject obj = JObject.Parse(json);
JsonSerializer serializer = new JsonSerializer();
serializer.Formatting = Formatting.Indented;
StringWriter sw = new StringWriter();
using (JsonTextWriter writer = new JsonTextWriter(sw))
{
writer.Formatting = Formatting.Indented;
writer.Indentation = 4;
writer.IndentChar = ' ';
serializer.Serialize(writer, obj);
}
return sw.ToString();
}
catch (JsonException)
{
return json;
}
}
private void MapJsonPositions(string json)
{
positionToPathMap.Clear();
JObject obj = JObject.Parse(json);
FillPositionMap(obj, "", json);
}
private void FillPositionMap(JToken token, string basePath, string json)
{
switch (token.Type)
{
case JTokenType.Object:
foreach (KeyValuePair<string, JToken> property in (JObject)token)
{
string keyPath = basePath + "['" + property.Key + "']";
int keyPosition = FindStringPosition(json, "\"" + property.Key + "\"");
if (keyPosition != -1)
{
for (int i = keyPosition; i < keyPosition + property.Key.Length + 2; i++)
{
positionToPathMap = keyPath;
}
}
FillPositionMap(property.Value, keyPath, json);
}
break;
case JTokenType.Array:
int index = 0;
foreach (JToken item in (JArray)token)
{
FillPositionMap(item, basePath + "[" + index + "]", json);
index++;
}
break;
}
}
private int FindStringPosition(string source, string value)
{
return source.IndexOf(value, StringComparison.Ordinal);
}
}
效果图
看到评论有人看不懂,简单解释下
就是点击某个key之后,在标题栏显示该key所在的位置,并复制到剪贴板
对爬虫来说很有用
介绍可以详细一点吗,感觉有点模糊不清 不错的插件哈 哈哈,谢谢楼主,正在学习中 获取B站所有的json数据? 学习,学习 这个我看看,不错啊 感谢分享,一起学习进步 不明觉厉,认真学习中,谢谢楼主分享。 大佬可以说的详细点吗
页:
[1]
2