【发布时间】:2019-09-27 11:44:46
【问题描述】:
我在 JSON 文件中有一个 Xpath 属性,我想从此 Xpath 中获取两个子字符串,以帮助这些子字符串转换为两个变量。
JSON对象如下;
{
'selectGateway':'0',
'waitingTime':'20000',
'status':'200',
'correlationID':'1',
'matchString':[{'xpath':'/whitelist/locations/location/var-fields/var-field[@key="whitelist-entry" and @value="8.0440147AA44A80"]','value':''}],
'matchInteger':[],
'matchSortedList':[]
}
这是我迄今为止的尝试,它工作正常,我只是在寻找一种方法来更动态地执行此操作,如果可能的话,以更好的方式。
int firstStringPositionForKey = matchString[index].xpath.IndexOf("@key=\"");
int secondStringPositionForKey = matchString[index].xpath.IndexOf("\" and");
string betweenStringForKey = matchString[index].xpath.Substring(firstStringPositionForKey+6, secondStringPositionForKey-firstStringPositionForKey-6);
int firstStringPositionForValue = matchString[index].xpath.IndexOf("@value=\"");
int secondStringPositionForValue = matchString[index].xpath.IndexOf("\"]");
string betweenStringForValue = matchString[index].xpath.Substring(firstStringPositionForValue+8, secondStringPositionForValue-firstStringPositionForValue-8);
我希望输出是这样的:
key is : whitelist-entry
value is : 8.0440147AA44A80
【问题讨论】:
-
到目前为止您尝试了什么?向我们展示你的尝试。提示:你可以使用
Split('/').Last() -
我已经在主要问题中添加了我的尝试,你可以检查一下。