简体   繁体   中英

How do I turn this Json file into an int array?

I have a program that calculates and returns the largest number inside of an array of integers.

I know how to return the largest number if the array is an int[]. But I am not sure how to return the largest number from a json file. The json file has to be entered as an argument inside of Main().

So the point is that when I run the application the program should return the number 7 when the json file is the argument inside of static void Main(string[] args).

Surely there is some command like dotnet run "data.json" if I want to run the json file as an argument from the class Program?

The program consist of 3 files. The main program file. The class Max, with the methods returnMax and showResult. And of course the json file.

I have read the newtonsoft documentation but haven't found anything valuable to solve this problem.

Could someone help me out?

The main Program code:

namespace json
{
  class Program
  {
    static void Main(string[] args)
    {
      // So the jason.file should be put as an argument inside of
      // the static void Main(string[] args).
      // And a 7 should be returned.
      string json = File.ReadAllText(args);
      int[] numbers = JsonConvert.DeserializeObject<int[]>(json);

      Console.WriteLine(Max.showResult(numbers));
    }
  }
}

The Max code:

namespace json
{
  public class Max
  {

    public static dynamic showResult(int[] source)
    { 
      return $"The largest number is: {returnMax(numbers)}";
    }
    public static int returnMax(int[] source)
    {

      int largestNumber = source.Max();
      return largestNumber;

    }
  }
}

The data.json file:

[ 1, 2, 3, 4, 5, 6, 7 ]

So I expect Console.WriteLine(Max.showResult(source)) to return 7 when I have the json file as an argument inside of static void Main(string[] args) .

If you are still in trouble I managed a solution and it's working (command line gives me 7 as biggest number). Here is what I've done: 1) I modified the json file:

{
  "numbers": [ 1, 2, 3, 4, 5, 6, 7 ]
}

I made this change because it is important to have a class that is similar to json file, otherwise Jsondeserializer won't work (this is what I've learned), so this is the corresponding class where I'll store json data:

namespace MaxNumberJson
{
    class JsonDeserializer
    {
    public List<int> numbers { get; set; }
    }
}

2) Then, because of using List<> I modified your methods in this way (I basically changed the parameters):

class MaxNumber
    {
        public string showResult(List<int> source)
        {
            return $"The largest number is: {returnMax(source)}";
        }
        public static int returnMax(List<int> source)
        {

            int largestNumber = source.Max();
            return largestNumber;

        }
    }

3) Finally I wrote some code on main.cs:

class Program
    {
        static void Main(string[] args)
        {
            JsonDeserializer jsonDeserializer = new JsonDeserializer();
            MaxNumber maxNumber = new MaxNumber();
            string json = File.ReadAllText(args[0]);
            jsonDeserializer = JsonConvert.DeserializeObject<JsonDeserializer>(json);

            Console.WriteLine(maxNumber.showResult(jsonDeserializer.numbers));
        }
    }

The first class (JsonDeserializer) is the class i created just to store json data form file; after that i put the content of the file passed by command line in a string and the I used the newtonsoft library and finally printed the output; Hope this could help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM