Base64解码从URL发送的json数组

时间:2022-11-13 11:12:16

I want to decode a simple json array using C# that's stored in a GitHub repo to see if it contains a value. I'm using the Newtonsoft json package. I've read this thread: Code for decoding/encoding a modified base64 URL, but I can't seem to implement the solution. I get the following error:

我想使用存储在GitHub存储库中的C#解码一个简单的json数组,以查看它是否包含值。我正在使用Newtonsoft json包。我已经读过这个帖子:用于解码/编码修改后的base64 URL的代码,但我似乎无法实现该解决方案。我收到以下错误:

System.FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters

System.FormatException:输入不是有效的Base-64字符串,因为它包含非基本64个字符,两个以上的填充字符或填充字符中的非法字符

, but I think there's also something going on with the code.

,但我认为代码也会发生一些事情。

var value = "somestring";
var encodedTopicTypeURL ="https://api.github.com/repos/org/repo1/contents/sample.json";
string decodedTopicTypeURL;
byte[] buffer = Convert.FromBase64String(encodedTopicTypeURL);
decodedTopicTypeURL = Encoding.UTF8.GetString(buffer);

using (var webClient = new System.Net.WebClient())
{
    var topicTypeJson = webClient.DownloadString(decodedTopicTypeURL);
    JArray validTopicTypes = JArray.Parse(topicTypeJson);
    if (!validTopicTypes.Contains(value))
    {
        Logger.LogError($"Value not found");
     }

Json array:

Json数组:

[
"string1",
"string2",
"string3",
"string4",              
]

2 个解决方案

#1


1  

Your question doesn't specify why you are trying to decode the URL from Base-64/ Actually encodedTopicTypeURL is just a normal string, why don't you use it directly as below?

您的问题没有说明为什么要尝试解码Base-64中的URL /实际编码.TopicTypeURL只是一个普通字符串,为什么不直接使用它?

var value = "somestring";
var url = "https://api.github.com/repos/org/repo1/contents/sample.json";

using (var webClient = new System.Net.WebClient())
{
    var topicTypeJson = webClient.DownloadString(url);
    JArray validTopicTypes = JArray.Parse(topicTypeJson);
    if (!validTopicTypes.Contains(value))
    {
        Logger.LogError($"Value not found");
     }
}

If you actually need to get the URL off of its base64 representation, then replace the following line

如果您确实需要从其base64表示中获取URL,请替换以下行

var encodedTopicTypeURL ="https://api.github.com/repos/org/repo1/contents/sample.json";

with this

有了这个

var encodedTopicTypeURL ="aHR0cHM6Ly9hcGkuZ2l0aHViLmNvbS9yZXBvcy9vcmcvcmVwbzEvY29udGVudHMvc2FtcGxlLmpzb24=";

This way decodedTopicTypeURL will actually have the url value.

这种方式decodeTopicTypeURL实际上将具有url值。

You can use this site to play around with Base64 encoding/decoding

您可以使用此站点来使用Base64编码/解码

#2


1  

https://api.github.com/repos/org/repo1/contents/sample.json is not Base64 encoded string. It's Base64 representation is aHR0cHM6Ly9hcGkuZ2l0aHViLmNvbS9yZXBvcy9vcmcvcmVwbzEvY29udGVudHMvc2FtcGxlLmpzb24=

https://api.github.com/repos/org/repo1/contents/sample.json不是Base64编码的字符串。它的Base64表示是aHR0cHM6Ly9hcGkuZ2l0aHViLmNvbS9yZXBvcy9vcmcvcmVwbzEvY29udGVudHMvc2FtcGxlLmpzb24 =

You can download data and when you have it in string variable(I used DownloadData(string url) to retrieve it parse it to JObject, take it Values and look for content inside.

您可以下载数据,当您在字符串变量中使用它时(我使用DownloadData(字符串url)检索它将其解析为JObject,将其取值并查找内部的内容。

For example:

例如:

    static void Main(string[] args)
    {
        string url = @"https://api.github.com/";
        string searchString = "url";
        WebClient wc = new WebClient();
        wc.Headers.Add("user-agent", "Mozilla / 4.0(compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
        try
        {
            byte[] dat = wc.DownloadData(url);
            string data = Encoding.ASCII.GetString(dat);
            var values = JObject.Parse(data).Values();
            bool stringFound = values.Any(x => x.Path.Contains(searchString) ||
                                               x.Value<string>().Contains(searchString)
                                         );
            if(stringFound)
            {
                Console.WriteLine("JSON contains search string");
            }
        }
        catch(Exception e)
        {
            throw;
        }

    }

I'm sure searching through values can be done better (;

我敢肯定,搜索价值可以做得更好(;

#1


1  

Your question doesn't specify why you are trying to decode the URL from Base-64/ Actually encodedTopicTypeURL is just a normal string, why don't you use it directly as below?

您的问题没有说明为什么要尝试解码Base-64中的URL /实际编码.TopicTypeURL只是一个普通字符串,为什么不直接使用它?

var value = "somestring";
var url = "https://api.github.com/repos/org/repo1/contents/sample.json";

using (var webClient = new System.Net.WebClient())
{
    var topicTypeJson = webClient.DownloadString(url);
    JArray validTopicTypes = JArray.Parse(topicTypeJson);
    if (!validTopicTypes.Contains(value))
    {
        Logger.LogError($"Value not found");
     }
}

If you actually need to get the URL off of its base64 representation, then replace the following line

如果您确实需要从其base64表示中获取URL,请替换以下行

var encodedTopicTypeURL ="https://api.github.com/repos/org/repo1/contents/sample.json";

with this

有了这个

var encodedTopicTypeURL ="aHR0cHM6Ly9hcGkuZ2l0aHViLmNvbS9yZXBvcy9vcmcvcmVwbzEvY29udGVudHMvc2FtcGxlLmpzb24=";

This way decodedTopicTypeURL will actually have the url value.

这种方式decodeTopicTypeURL实际上将具有url值。

You can use this site to play around with Base64 encoding/decoding

您可以使用此站点来使用Base64编码/解码

#2


1  

https://api.github.com/repos/org/repo1/contents/sample.json is not Base64 encoded string. It's Base64 representation is aHR0cHM6Ly9hcGkuZ2l0aHViLmNvbS9yZXBvcy9vcmcvcmVwbzEvY29udGVudHMvc2FtcGxlLmpzb24=

https://api.github.com/repos/org/repo1/contents/sample.json不是Base64编码的字符串。它的Base64表示是aHR0cHM6Ly9hcGkuZ2l0aHViLmNvbS9yZXBvcy9vcmcvcmVwbzEvY29udGVudHMvc2FtcGxlLmpzb24 =

You can download data and when you have it in string variable(I used DownloadData(string url) to retrieve it parse it to JObject, take it Values and look for content inside.

您可以下载数据,当您在字符串变量中使用它时(我使用DownloadData(字符串url)检索它将其解析为JObject,将其取值并查找内部的内容。

For example:

例如:

    static void Main(string[] args)
    {
        string url = @"https://api.github.com/";
        string searchString = "url";
        WebClient wc = new WebClient();
        wc.Headers.Add("user-agent", "Mozilla / 4.0(compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
        try
        {
            byte[] dat = wc.DownloadData(url);
            string data = Encoding.ASCII.GetString(dat);
            var values = JObject.Parse(data).Values();
            bool stringFound = values.Any(x => x.Path.Contains(searchString) ||
                                               x.Value<string>().Contains(searchString)
                                         );
            if(stringFound)
            {
                Console.WriteLine("JSON contains search string");
            }
        }
        catch(Exception e)
        {
            throw;
        }

    }

I'm sure searching through values can be done better (;

我敢肯定,搜索价值可以做得更好(;