这是一个全新的问题,我已经纠结了几个小时了!
我试图了解如何实际使用,并创建JSON数据。我整个下午都在谷歌上搜索,试图理解我在这里找到的东西http://james.newtonking.com/projects/json/help/下载了Newtonsoft dll。
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
using (JsonWriter jsonWriter = new JsonTextWriter(sw))
{
jsonWriter.Formatting = Formatting.Indented;
jsonWriter.WriteStartObject();
jsonWriter.WritePropertyName("CPU");
jsonWriter.WriteValue("Intel");
jsonWriter.WritePropertyName("PSU");
jsonWriter.WriteValue("500W");
jsonWriter.WritePropertyName("Drives");
jsonWriter.WriteStartArray();
jsonWriter.WriteValue("DVD read/writer");
jsonWriter.WriteComment("(broken)");
jsonWriter.WriteValue("500 gigabyte hard drive");
jsonWriter.WriteValue("200 gigabype hard drive");
jsonWriter.WriteEnd();
jsonWriter.WriteEndObject();
}
应该创建如下内容:
{
"CPU": "Intel",
"PSU": "500W",
"Drives": [
"DVD read/writer"
/*(broken)*/,
"500 gigabyte hard drive",
"200 gigabype hard drive" ]
}
,我确信它…但我怎么看呢?如何将其转换为浏览器可以输出的对象
在我看来,我需要解决的第一阶段是"如何创建"JSON文件/字符串,下一阶段将是如何实际使用它们。如果它有助于回答这个问题,我最初的目标是能够从MySQL数据库生成的搜索页面中使用AJAX自动完成,我希望我可以编写一个简单的SQL查询,并使用类似于上面的东西返回,但我显然是错的!
顺便说一句,上面的例子是在c#中,我已经成功地将过程转换为VB,因为这就是我正在使用的,但任何回应都会非常感谢VB的例子!
我在这篇文章发表两年后偶然发现了它,但我也有同样的问题,并注意到这个问题并没有得到真正的回答。为了回答OP的问题,这将为您提供示例中的JSON字符串。
sb.toString()
结果是您需要将JSON字符串返回给浏览器。您可以将其放在javascript变量中(如果这样做,请确保清除行尾和单引号),也可以将其作为ajax查询的结果传递回去。
我们实际上使用内置的Javascript序列化器,因为它在服务器端和客户端都有支持,而且非常容易使用。假设您有一个现有的对象,下面的代码放在服务器端:
''' <summary>
''' This method safely serializes an object for JSON by removing all of the special characters (i.e. CRLFs, quotes, etc)
''' </summary>
''' <param name="oObject"></param>
''' <param name="fForScript">Set this to true when the JSON will be embedded directly in web page (as opposed to being passed through an ajax call)</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function SerializeObjectForJSON(ByVal oObject As Object, Optional ByVal fForScript As Boolean = False) As String
If oObject IsNot Nothing Then
Dim sValue As String
sValue = (New System.Web.Script.Serialization.JavaScriptSerializer).Serialize(oObject)
If fForScript Then
' If this serialized object is being placed directly on the page, then we need to ensure that its CRLFs are not interpreted literlally (i.e. as the actual JS values)
' If we don't do this, the script will not deserialize correctly if there are any embedded crlfs.
sValue = sValue.Replace("rn", "\r\n")
' Fix quote marks
Return CleanString(sValue)
Else
Return sValue
End If
Else
Return String.Empty
End If
End Function
在客户端,反序列化是微不足道的:
// The result should be a json-serialized record
oRecord = Sys.Serialization.JavaScriptSerializer.deserialize(result.value);
一旦你反序列化了对象,你就可以直接在javascript中使用它的属性了:
alert('CPU = ' + oRecord.CPU);
在生成JSON方面尝试
public class HardwareInfo
{
[JsonProperty(PropertyName = "CPU")]
public string Cpu { get; set; }
[JsonProperty(PropertyName = "PSU")]
public string Psu { get; set; }
[JsonProperty]
public ICollection<string> Drives { get; set; }
}
public string SerializeHardwareInfo()
{
var info = new HardwareInfo
{
Cpu = "Intel",
Psu = "500W",
Drives = new List<string> { "DVD read/writer", "500 gigabyte hard drive", "200 gigabype hard drive" }
};
var json = JsonConvert.SerializeObject(info, Formatting.Indented);
// {
// "CPU": "Intel",
// "PSU": "500W",
// "Drives": [
// "DVD read/writer",
// "500 gigabyte hard drive",
// "200 gigabype hard drive"
// ]
// }
return json;
}
format参数是可选的。祝你好运。