C# 获取json文件的最后一个元素
使用工作代码编辑2020年6月30日。谢谢大家!强> 嗨,我想获取解决方案中json文件的最后一个元素。 实际上,我的Web服务发送所有json文件:C# 获取json文件的最后一个元素,c#,.net,json,visual-studio,web-services,C#,.net,Json,Visual Studio,Web Services,使用工作代码编辑2020年6月30日。谢谢大家! 嗨,我想获取解决方案中json文件的最后一个元素。 实际上,我的Web服务发送所有json文件: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.Data; using Newtonsoft.Json; using System.Text; us
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using Newtonsoft.Json;
using System.Text;
using System.IO;
using Newtonsoft.Json.Linq;
namespace getLastVersionNumber
{
/// <summary>
/// Description résumée de WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// Pour autoriser l'appel de ce service Web depuis un script à l'aide d'ASP.NET AJAX, supprimez les marques de commentaire de la ligne suivante.
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string IDVersion()
{
//JSON path
String json = File.ReadAllText(@"C:\Users\sejjilali\source\repos\WebserviceProdware\getLastVersionNumber\idVersions.json");
//Parsing, get last one and convert it to a string
var lastVersion = JArray.Parse(json).Last().ToString();
return lastVersion;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Services;
使用系统数据;
使用Newtonsoft.Json;
使用系统文本;
使用System.IO;
使用Newtonsoft.Json.Linq;
命名空间getLastVersionNumber
{
///
///网站服务说明1
///
[WebService(命名空间=”http://tempuri.org/")]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
//将自动登录的appel de ce服务Web部门与ASP.NET AJAX脚本相结合,支持在线评论。
//[System.Web.Script.Services.ScriptService]
公共类WebService 1:System.Web.Services.WebService
{
[网络方法]
公共字符串IDVersion()
{
//JSON路径
字符串json=File.ReadAllText(@“C:\Users\sejjilali\source\repos\WebserviceProdware\getLastVersionNumber\idVersions.json”);
//解析,获取最后一个并将其转换为字符串
var lastVersion=JArray.Parse(json.Last().ToString();
返回最新版本;
}
}
}
这是我的文件样本:
[
{
“id”:1,
“版本”:0.1
},
{
“id”:2,
“版本”:0.5
},
{
“id”:3,
“版本”:0.8
}
]
我只想得到最后一个{“id”:3,“version”:0.8}
thanx需要帮助首先定义这样的类
public class Data{
public int id{get;set;}
public double version{get;set;}
}
List<Data> myDatas=JsonConvert.DeserializeObject<List<Account>>(stringfromwebservice);
Data myData=myDatas.Last();
在那之后,你可以像这样得到最后一个值
public class Data{
public int id{get;set;}
public double version{get;set;}
}
List<Data> myDatas=JsonConvert.DeserializeObject<List<Account>>(stringfromwebservice);
Data myData=myDatas.Last();
List myDatas=JsonConvert.DeserializeObject(stringfromwebservice);
Data myData=myDatas.Last();
用法:
var stringfromwebservice="from web server data";
List<webservice> webserviceList=JsonConvert.DeserializeObject<List<webservice>>(stringfromwebservice);
webservice DataModel = webserviceList[webserviceList.Count];
您可以尝试以下代码来获取json文件的最后一个元素
string path = "D:\\1.json";
string text = File.ReadAllText(path);
JToken test = JArray.Parse(text).Last();
Console.WriteLine(test);
结果:
JArray.Parse(st.Last()
;嗨,埃文,我试过了,但没用。JArray test=JArray.Parse(json.Last();最后停留在红色谢谢你的帮助sadullah,我正在阅读你的答案,但我如何使用它?你有我能读的主题吗?请提供一些上下文信息。为什么你认为这段代码应该对OP.HI有帮助?杰克刚刚用这个解决方案编辑了代码。你的帮助了我谢谢你!