내 프롬프트는 다음과 같습니다.
원격 URL에서 JSON 파일을 검색합니다. 솔루션은 설정 파일 (app.config, web.config 등)에서이를 가져와야합니다. (URL이 있습니다)
제공된 문자열이 회문인지 확인합니다. 문자열이 회문인지 여부를 평가할 때 영숫자 문자가 고려됩니다.
저는 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"
}
]
}
URL에서 json 문자열을 얻는 방법은 다음과 같습니다. URL에서 json 문자열 을 얻는 방법?
다음은 JSON을 역 직렬화하는 방법입니다 . 개체 역 직렬화
다음은 문자열이 회문인지 확인하는 함수를 작성하는 방법입니다. 문자열이 회문 인지 확인합니다.
당신은 올바른 길을 가고 있습니다. 아래에서 필요한 것을 얻기 위해 코드를 업데이트했습니다.
class Program
{
private static void Main(string[] args)
{
// Get JSON from URL
var json = GetJasonFromUrl(Properties.Settings.Default.url);
// De-serialize JSON into a list
var deserlizedJson = DeserializeMyJson(json);
// Go through each item in the list and determine if palindrome or not
foreach (var item in deserlizedJson)
{
if (IsPalindrome(item.Str))
Console.WriteLine(item.Str + " is palindrome");
else
Console.WriteLine(item.Str + " is not palindrome");
}
}
private static string GetJasonFromUrl(string url)
{
string result;
try
{
using (var webClient = new WebClient())
{
result = webClient.DownloadString(url);
}
}
catch (Exception)
{
result = string.Empty;
}
return result;
}
private static IEnumerable<Palindromes> DeserializeMyJson(string json)
{
return JsonConvert.DeserializeObject<IEnumerable<Palindromes>>(json);
}
// Assuming your function is tested and correct
private static bool IsPalindrome(string value)
{
var min = 0;
var max = value.Length - 1;
while (true)
{
if (min > max)
return true;
var a = value[min];
var b = value[max];
if (char.ToLower(a) != char.ToLower(b))
return false;
min++;
max--;
}
}
}
internal class Palindromes
{
public string Str { get; set; } = string.Empty;
public bool Result { get; set; } = false;
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다