简体   繁体   English

如何将此Json文件转换为int数组?

[英]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[]. 我知道如何在数组为int []的情况下返回最大的数字。 But I am not sure how to return the largest number from a json file. 但是我不确定如何从json文件返回最大的数字。 The json file has to be entered as an argument inside of Main(). 必须在Main()内部将json文件作为参数输入。

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). 所以关键是当我运行应用程序时,当json文件是static void Main(string [] args)内部的参数时,程序应返回数字7。

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? 如果我想将json文件作为类Program的参数运行,是否肯定有一些命令,例如dotnet run“ data.json”?

The program consist of 3 files. 该程序包括3个文件。 The main program file. 主程序文件。 The class Max, with the methods returnMax and showResult. Max类,带有方法returnMax和showResult。 And of course the json file. 当然还有json文件。

I have read the newtonsoft documentation but haven't found anything valuable to solve this problem. 我已经阅读了newtonsoft文档,但没有发现任何有价值的解决该问题的方法。

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: data.json文件:

[ 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) . 因此,当我将json文件作为static void Main(string[] args)的参数时,我希望Console.WriteLine(Max.showResult(source))返回7。

If you are still in trouble I managed a solution and it's working (command line gives me 7 as biggest number). 如果您仍然遇到问题,请尝试管理一个解决方案,并且该解决方案可以正常工作(命令行为我提供了7个最大的解决方案)。 Here is what I've done: 1) I modified the json file: 这是我已经完成的工作:1)我修改了json文件:

{
  "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: 我进行此更改是因为拥有与json文件类似的类很重要,否则Jsondeserializer将无法工作(这是我所学的),因此这是我将在其中存储json数据的相应类:

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): 2)然后,由于使用了List <>,我以这种方式修改了您的方法(我基本上改变了参数):

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: 3)最后,我在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; 第一类(JsonDeserializer)是我创建的用于存储json数据表单文件的类; 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; 之后,我将通过命令行传递的文件的内容放在字符串中,然后我使用了newtonsoft库,最后打印了输出; Hope this could help! 希望这会有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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