【问题标题】:Base64 decode json array sent from URL从 URL 发送的 Base64 解码 json 数组
【发布时间】:2016-08-16 06:02:56
【问题描述】:

我想使用 C# 对存储在 GitHub 存储库中的简单 json 数组进行解码,以查看它是否包含值。我正在使用 Newtonsoft json 包。我读过这个帖子:Code for decoding/encoding a modified base64 URL,但我似乎无法实现该解决方案。我收到以下错误:

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

,但我认为代码也有问题。

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",              
]

【问题讨论】:

    标签: c# json


    【解决方案1】:

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

    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,则替换以下行

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

    有了这个

    var encodedTopicTypeURL ="aHR0cHM6Ly9hcGkuZ2l0aHViLmNvbS9yZXBvcy9vcmcvcmVwbzEvY29udGVudHMvc2FtcGxlLmpzb24=";
    

    这样decodedTopicTypeURL实际上会有url值。

    您可以使用this 站点来玩转 Base64 编码/解码

    【讨论】:

      【解决方案2】:

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

      你可以下载数据,当你把它放在字符串变量中时(我用DownloadData(string url)来检索它,将它解析为JObject,取它的值并在里面寻找内容。

      例如:

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

      我确信通过值搜索可以做得更好(;

      【讨论】:

      • 是的,我在 GitHub 中的 json 文件的 URL 中看到了 Base64 表示。当您声明它不是字符串时,我想知道的是如何正确解码 JSON 以访问键以检查值。如果您有显示如何执行此操作的 URL 或基于我上面的问题和代码的代码示例。
      猜你喜欢
      • 1970-01-01
      • 2013-02-27
      • 2013-08-31
      • 1970-01-01
      • 2019-10-21
      • 2020-10-05
      • 1970-01-01
      • 1970-01-01
      • 2016-11-03
      相关资源
      最近更新 更多