【发布时间】:2020-01-03 18:10:11
【问题描述】:
这是我的提示:
从远程 URL 检索 JSON 文件。您的解决方案应该从设置文件(app.config、web.config 等)中提取它。 (我有网址)
确定提供的字符串是否为回文。在评估字符串是否为回文时,将考虑字母数字字符。
- 解析检索到的 JSON 文件,并将“字符串”数组中的每个元素传递给步骤 #2 中的函数。您应该打印出字符串和结果。
我是 C# 新手,我无法弄清楚如何从 url 读取 json 文件,然后将其用于函数。我几乎被困在如何开始这个问题上。有什么建议吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
namespace ConsoleApplication1
{
class Program
{
public static bool IsPalindrome(string value)
{
int min = 0;
int max = value.Length - 1;
// while (true)
{
if (min > max)
{
return true;
}
char a = value[min];
char b = value[max];
if (char.ToLower(a) != char.ToLower(b))
{
return false;
}
min++;
max--;
}
}
static void Main() {
using (WebClient webClient = new System.Net.WebClient())
{
WebClient n = new WebClient();
var json = n.DownloadString("URL");
string valueOriginal = Convert.ToString(json);
//Console.WriteLine(json);
}
string[] array = {
};
foreach (string value in array)
{
Console.WriteLine("{0} = {1}", value, IsPalindrome(value));
}
}
}
}
示例 JSON:
{
"strings": [
{
"str": "mom",
"result": "true"
},
{
"str": "Taco Cat",
"result": "true"
},
{
"str": "university",
"result": "false"
},
{
"str": "Amore, Roma.",
"result": "true"
},
{
"str": "King are you glad you are king",
"result": "false"
}
]
}
【问题讨论】:
-
要解析 JSON,请阅读 How can I parse JSON with C#? 或 How to read JSON data?(对于 LINQ to JSON)。老实说,在互联网上快速搜索“如何在 c# 中读取 json”将为您提供足够多的阅读材料来弄清楚......
-
Convert JSON String To C# Object 和 How to auto-generate a C# class file from a JSON object string 应该完全回答您关于如何反序列化 JSON 的问题。如果您在回文检测方面需要帮助,您应该问另一个问题,因为这里的首选格式是 one question per post。
-
这能回答你的问题吗? How can I parse JSON with C#?
标签: c# json palindrome