有没有办法知道在C#中迭代和分析JSON字符串时哪个是最后一个令牌?

我正在解析字符串上的一些JSON数据,以将其传递到具有自定义格式的文件。但是我需要知道哪个是文件的最后一个标记,以便我用“}”而不是“}”关闭标记(这将是,没有逗号才能获得正确的语法)。


我试图使用该属性获取最后一个令牌,但它并不像我想象的那样工作。Last


这是我用来解析数据并写入文件的函数(它还将所需的数据输出到终端,但不是在JSON数据中,这就是为什么我首先解析它):


private static void ParseData()

        {

            JObject jsonData;


            try

            {

                using (StreamWriter file = new StreamWriter(Globals.dataFile))

                {

                    file.WriteLine("{");


                    jsonData = JObject.Parse(Globals.json);

                    Console.WriteLine("*********************************************");

                    foreach (JToken tokens in jsonData["indicator"]["values"])

                    {

                        Console.WriteLine("Fecha y hora: " + tokens["datetime"] + " Valor: " + tokens["value"] + Environment.NewLine + "*********************************************");

                        file.Write(Environment.NewLine + "\t{" + Environment.NewLine + "\t\t\"datetime\": " + tokens["datetime"] + ",");

                        file.WriteLine("\t\t\"value\": " + tokens["value"]);

                        if (tokens != tokens.Last)

                        {

                            file.WriteLine("\t},");

                            Console.WriteLine("\t},");

                        }

                        else

                        {

                            Console.WriteLine("\t}");

                        }

                    }


                    file.WriteLine("}");

                }

            }

            catch (Exception ex)

            {

                PrintExceptionMessage(ex);

            }

        }


拉莫斯之舞
浏览 90回答 1
1回答

翻过高山走不出你

您需要将所选键/值对从json解析为类对象列表,然后再次将其保存到新文件中,例如private static void ParseData(){&nbsp; &nbsp; string json = File.ReadAllText(@"Path to your input json file");&nbsp; &nbsp; JObject jsonData = JObject.Parse(json);&nbsp; &nbsp; List<MyCustomClass> output = new List<MyCustomClass>();&nbsp; &nbsp; foreach (JToken tokens in jsonData["indicator"]["values"])&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; output.Add(new MyCustomClass { datetime = tokens["datetime"].ToString(), value = tokens["value"].ToString() });&nbsp; &nbsp; }&nbsp; &nbsp; File.WriteAllText(@"Path to your output json file", JToken.FromObject(output).ToString());}你需要一个类对象来解析你的键/值对,class MyCustomClass{&nbsp; &nbsp; public string datetime { get; set; }&nbsp; &nbsp; public string value { get; set; }}输出:[&nbsp; {&nbsp; &nbsp; "datetime": "31-01-2019 05:30:00 AM",&nbsp; &nbsp; "value": "49.38"&nbsp; },&nbsp; {&nbsp; &nbsp; "datetime": "31-01-2019 06:30:00 AM",&nbsp; &nbsp; "value": "47.46"&nbsp; },&nbsp; {&nbsp; &nbsp; "datetime": "31-01-2019 07:30:00 AM",&nbsp; &nbsp; "value": "46.82"&nbsp; },&nbsp; {&nbsp; &nbsp; "datetime": "31-01-2019 08:30:00 AM",&nbsp; &nbsp; "value": "46.5"&nbsp; },&nbsp; {&nbsp; &nbsp; "datetime": "31-01-2019 09:30:00 AM",&nbsp; &nbsp; "value": "47.36"&nbsp; },&nbsp; {&nbsp; &nbsp; "datetime": "31-01-2019 10:30:00 AM",&nbsp; &nbsp; "value": "50.05"&nbsp; },&nbsp; {&nbsp; &nbsp; "datetime": "31-01-2019 11:30:00 AM",&nbsp; &nbsp; "value": "56.61"&nbsp; },&nbsp; {&nbsp; &nbsp; "datetime": "31-01-2019 12:30:00 PM",&nbsp; &nbsp; "value": "65.58"&nbsp; },&nbsp; {&nbsp; &nbsp; "datetime": "31-01-2019 01:30:00 PM",&nbsp; &nbsp; "value": "66.59"&nbsp; },&nbsp; {&nbsp; &nbsp; "datetime": "31-01-2019 02:30:00 PM",&nbsp; &nbsp; "value": "66.49"&nbsp; },&nbsp; {&nbsp; &nbsp; "datetime": "31-01-2019 03:30:00 PM",&nbsp; &nbsp; "value": "65.54"&nbsp; },&nbsp; {&nbsp; &nbsp; "datetime": "31-01-2019 04:30:00 PM",&nbsp; &nbsp; "value": "63.71"&nbsp; },&nbsp; {&nbsp; &nbsp; "datetime": "31-01-2019 05:30:00 PM",&nbsp; &nbsp; "value": "61.94"&nbsp; },&nbsp; {&nbsp; &nbsp; "datetime": "31-01-2019 06:30:00 PM",&nbsp; &nbsp; "value": "56.58"&nbsp; },&nbsp; {&nbsp; &nbsp; "datetime": "31-01-2019 07:30:00 PM",&nbsp; &nbsp; "value": "56.08"&nbsp; }]
打开App,查看更多内容
随时随地看视频慕课网APP