Here is my prompt:
Retrieve a JSON file from a remote URL. Your solutions should pull this from a settings file (app.config, web.config. etc). (I have the url)
Determine if a provided string is a palindrome. Alphanumeric chars will be considered when evaluating whether or not the string is a palindrome.
I am new to C# and I am having trouble figuring out how to read the json file from the url, and then using it for the function. I am pretty much stuck on how to start this. Any tips?
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));
}
}
}
}
Sample 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"
}
]
}
Here is how you can get a json string from a URL:How to get a json string from url?
Here is how you can deserialize a json: Deserialize an Object
Here is how you can write a function to check if a string is palindrome:Check if a string is a palindrome
You are on the right track. Below I have updated your code to get what you need:
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;
}