1

Here is my prompt:

  1. 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)

  2. Determine if a provided string is a palindrome. Alphanumeric chars will be considered when evaluating whether or not the string is a palindrome.

  3. Parse the retrieved JSON file, and pass each element in the "strings" array, into the function in step #2. You should print out the string and result.

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"
    }
  ]
}
Shahzad
  • 2,033
  • 1
  • 16
  • 23
idkidk
  • 61
  • 5
  • 6
    To parse the JSON, read [How can I parse JSON with C#?](https://stackoverflow.com/q/6620165/215552) or [How to read JSON data?](https://stackoverflow.com/q/44017807/215552) (for LINQ to JSON). Honestly, a quick search of the internet for "how to read json in c#" will get you more than enough reading material to figure it out... – Heretic Monkey Jan 03 '20 at 18:18
  • 2
    *[Convert JSON String To C# Object](https://stackoverflow.com/a/14904115/3744182)* and [*How to auto-generate a C# class file from a JSON object string*](https://stackoverflow.com/q/21611674/3744182) should fully answer your question about how to deserialize JSON. If you need help with palindrome detection you should ask another question, as the preferred format here is [one question per post](https://meta.stackexchange.com/q/222735). – dbc Jan 03 '20 at 18:33
  • Does this answer your question? [How can I parse JSON with C#?](https://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c) – Peter O. Jan 03 '20 at 19:44

1 Answers1

1

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;
}
amindomeniko
  • 417
  • 6
  • 23
  • I get an error with 'Properties' when getting the json from the url – idkidk Jan 03 '20 at 20:25
  • I assumed you are placing the URL in the application settings of your project. You can replace it with a hard-coded value for now until you decide where to place it so that in future you can change it without having to touch the code. Settings is under Properties of the project. – amindomeniko Jan 03 '20 at 20:27