我希望在下面的方法中,基于 attrNameListCurrent.Length
, else if else
代码块。换句话说,如果 attrNameListCurrent.Length = 2
我只需要(attrNameListCurrent [0]!= attrNameListPrevious [0])
和
<代码>否则&#XA; {&#XA; result + = AddItem(attrList [i]);&#xA; if(i == attrList.Count - 1)&#xA; {&#XA; result = result.TrimEnd(',');&#xA; result + = CloseObject(attrList [i]); ///&#XA; }&#xA;&#xA;&#xA;
代码的一部分。
&#xA;&#xA;如果 attrNameListCurrent.Length = 3
我想添加 else if(attrNameListCurrent [1]!= attrNameListPrevious [1])
它们之间的代码块
if attrNameListCurrent.Length = 4
我想添加 else if(attrNameListCurrent [2]!= attrNameListPrevious [2])
代码块等等。
有没有办法以编程方式执行此操作?请提出任何建议。&#xA;我的方法:
&#xA;&#xA; 公共静态字符串ReadRsdToJson(List&lt; AttributeProperties&gt; attrList)&#xA; {&#XA; string result =“{”;&#xA; bool createNew = true;&#xA; string attrName = string.Empty;&#xA; string [] attrNameListCurrent;&#xA; string [] attrNameListPrevious; &#XA; if(attrList.Count == 0)result =“{}”; //测试-1;列表是空的&#xA; for(var i = 0; i&lt; attrList.Count; i ++)&#xA; {&#XA; if(attrList [i] .XPath!= null)&#xA; {&#XA; if(i == 0)result + = CreateNewObject(attrList [i]);&#xA; if(i == 0&amp;&amp; i == attrList.Count - 1)result + = CloseObject(attrList [i]);&#xA;&#xA; attrNameListCurrent = attrList [i] .XPath.Split('/');&#xA; if(i!= 0)&#xA; {&#XA; attrNameListPrevious = attrList [i - 1] .XPath.Split('/'); &#XA; if(attrNameListCurrent [0]!= attrNameListPrevious [0])&#xA; {&#XA; result = result.TrimEnd(','); //关闭上一个对象&#xA; if(attrNameListCurrent [0]!= attrList [i-1] .XPath.Split('/')[0])result + = CloseObject(attrList [i-1]); //创建新对象&#xA; result + = CreateNewObject(attrList [i]);&#xA; if(i == attrList.Count-1)&#xA; {&#XA; result = result.TrimEnd(',');&#xA; result + = CloseObject(attrList [i]); ///&#XA; &#xA; }&#XA; else if(attrNameListCurrent [1]!= attrNameListPrevious [1])&#xA; {&#XA; result = result.TrimEnd(',');&#xA; if(attrNameListCurrent [1]!= attrList [i - 1] .XPath.Split('/')[1])result + = CloseNestedNthObject(attrList [i-1],1);&#xA; result + = CreateNestedNthElement(attrList [i],1);&#xA; if(i == attrList.Count - 1)&#xA; {&#XA; result = result.TrimEnd(',');&#xA; result + = CloseObject(attrList [i]);&#xA; &#xA; }&#XA; else if(attrNameListCurrent [2]!= attrNameListPrevious [2])&#xA; {&#XA; result = result.TrimEnd(',');&#xA; if(attrNameListCurrent [2]!= attrList [i - 1] .XPath.Split('/')[2])result + = CloseNestedNthObject(attrList [i-1],2);&#xA; result + = CreateNestedNthElement(attrList [i],2);&#xA; if(i == attrList.Count - 1)&#xA; {&#XA; result = result.TrimEnd(',');&#xA; result + = CloseObject(attrList [i]);&#xA; &#xA; }&#XA; else if(attrNameListCurrent [3]!= attrNameListPrevious [3])&#xA; {&#XA; result = result.TrimEnd(',');&#xA; if(attrNameListCurrent [3]!= attrList [i - 1] .XPath.Split('/')[3])result + = CloseNestedNthObject(attrList [i-1],3);&#xA; result + = CreateNestedNthElement(attrList [i],3);&#xA; if(i == attrList.Count - 1)&#xA; {&#XA; result = result.TrimEnd(',');&#xA; result + = CloseObject(attrList [i]);&#xA; &#xA; }&#XA;否则&#XA; {&#XA; result + = AddItem(attrList [i]);&#xA; if(i == attrList.Count - 1)&#xA; {&#XA; result = result.TrimEnd(',');&#xA; result + = CloseObject(attrList [i]); ///&#XA; &#xA; }&#XA; &#xA; }&#XA;否则&#XA; {&#XA; if(attrList [i] .Type ==“integer”|| attrList [i] .Aggregate!= null)&#xA; {&#XA;结果+ ='“'+ attrList [i] .Name +'”'+“:”+ attrList [i] .Value +',';&#xA; }&#XA;否则&#XA; {&#XA;结果+ ='“'+ attrList [i] .Name +'”'+“:”+'“'+ attrList [i] .Value +'”'+',';&#xA; }&#XA; }&#XA; if(i == attrList.Count - 1)&#xA; {&#XA; result = result.TrimEnd(',');&#xA;结果+ ='}';&#xA; }&#XA; }&#XA;返回结果;&#xA; }&#XA; 代码>
&#XA;
答案 0 :(得分:1)
你有没有关于使用switch语句?如果我理解正确的话,听起来可能就是你所需要的。
像
这样的东西switch (attrNameListCurrent.Length)
{
case 1:
#if length is 1 do thing
break;
case 2:
#if length is 2 do thing
break;
case 3:
#if length is 3 do thing
break;
}
答案 1 :(得分:0)
如果您要做的是从路径+值列表构建树,然后将其序列化为JSON,则构建树然后序列化将更容易。假设您有一个TreeNode
类,其中存储了子TreeNodes
和Value
的列表。假设它支持索引器,因此您可以使用[]
访问子节点。然后你可以做这样的事情:
public static string ReadRsdToJson(List<AttributeProperties> attrList)
{
var root = new TreeNode();
foreach (var attr in attrList)
{
var pathElements = attr.XPath.Split('/');
var current = root;
// Scan down the path following or building the tree at each step
foreach (var pathElement in pathElements)
{
if (current.ContainsKey(pathElement))
current = current[pathElement];
else
{
var newChild = new TreeNode(pathElement);
current.AddChild(newChild);
current = newChild;
}
}
// current is now the node at leaf of the tree that you want
current.Value = attr.Value;
}
// And now you have built a tree you can serialize that to Json
// If you must write your own serializer instead of using JSON.NET
// consider using string.Join(",",...) instead of trimming commas
}
它与您的代码完全不符,但对于SO问题,您的代码实际上太长且太复杂了。最好将其简化为您所要求的本质。