这是我的困境.我正在使用RESTful ASP.NET服务,尝试使用以下格式返回JSON字符串的函数:
{"Test1Key":"Test1Value","Test2Key":"Test2Value","Test3Key":"Test3Value"}
Run Code Online (Sandbox Code Playgroud)
但我会以这种格式得到它:
[{"Key":"Test1Key","Value":"Test1Value"},
{"Key":"Test2Key","Value":"Test2Value"},
{"Key":"Test3Key","Value":"Test3Value"}]
Run Code Online (Sandbox Code Playgroud)
我的方法看起来像这样:
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public Dictionary<string, string> Test(String Token)
{
if (!IsAuthorized(Token))
return null;
if (!IsSecure(HttpContext.Current))
return null;
Dictionary<string, string> testresults = new Dictionary<string, string>();
testresults.Add("Test1Key", "Test1Value");
testresults.Add("Test2Key", "Test2Value");
testresults.Add("Test3Key", "Test3Value");
return testresults;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法只使用内置的ASP.NET工具摆脱那些"Key"和"Value"标签?(即,如果可以避免的话,我宁愿不使用JSON.NET)
非常感谢!:)
我在java中解析一些XML时遇到了一些麻烦.我的XML文件被正确读取,我可以使用节点的getTextContent()函数从文件中获得大部分信息而没有任何问题(例如,在xml片段中显示的StreamType节点).
但是,当我尝试使用节点的子节点时,getNodeValue()和getTextContent()都返回此随机值:"\n\t\t".
NodeList propertyNodes似乎已正确填充(包含所有18个"Property"元素).
这是我代码中的代码段:
Document document;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(appXML);
...
String typeName = document.getElementsByTagName("StreamType").item(0).getTextContent();
...
String limit = "-1";
NodeList propertyNodes = document.getElementsByTagName("Property");
int nodelistlength = propertyNodes.getLength();
for (int i = 0; i < nodelistlength; i++) {
Node currentNode = propertyNodes.item(i);
Node nameNode = currentNode.getFirstChild();
Node valueNode = currentNode.getLastChild();
String name = nameNode.getNodeValue();
String value = valueNode.getNodeValue();
if (nameNode.getTextContent().equalsIgnoreCase("maxConnections"))
limit = valueNode.getTextContent();
}
Run Code Online (Sandbox Code Playgroud)
这里有一些来自我试图解析的XML的剪辑:
<Root>
<Application>
<Streams>
<StreamType>live</StreamType>
...
</Streams>
... …Run Code Online (Sandbox Code Playgroud) 我正在试图弄清楚如何在ASP.NET项目中自动生成单个c#文件的文档.我正在使用VS 2010,我有SandCastle,但我开始觉得它只能用于一个完整的项目.问题是我们整个项目都是巨大的,只有一个文件需要文档(它是唯一一个具有我们客户使用的功能 - 其他一切都是内部的)
我可以做一个批处理脚本,但是如果有更好的方法可以解决这个问题,我宁愿不要这样做!
有人有什么想法吗?首先十分感谢!
(作为旁注,我已经看到了这一点,并没有帮助:在WebSite项目中为单个VB类生成XML文档)