从URL发送的Base64解码json数组 [英] Base64 decode json array sent from URL

查看:339
本文介绍了从URL发送的Base64解码json数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

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

,但是我认为代码中也发生了一些事情.

, 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数组:

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

推荐答案

您的问题没有指定您为什么要尝试从Base-64解码URL/实际上encodeTopicTypeURL只是一个普通字符串,为什么不使用是否直接如下所示?

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?

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");
     }
}

如果您实际上需要从其base64表示形式获取该URL,则替换以下行

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

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

与此

var encodedTopicTypeURL ="aHR0cHM6Ly9hcGkuZ2l0aHViLmNvbS9yZXBvcy9vcmcvcmVwbzEvY29udGVudHMvc2FtcGxlLmpzb24=";

通过这种方式,decodedTopicTypeURL实际上将具有url值.

This way decodedTopicTypeURL will actually have the url value.

您可以使用网站来进行Base64编码/解码

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

这篇关于从URL发送的Base64解码json数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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