本文整理汇总了C#中JsonTextWriter.WritePropertyName方法的典型用法代码示例。如果您正苦于以下问题:C# JsonTextWriter.WritePropertyName方法的具体用法?C# JsonTextWriter.WritePropertyName怎么用?C# JsonTextWriter.WritePropertyName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JsonTextWriter
的用法示例。
在下文中一共展示了JsonTextWriter.WritePropertyName方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Example
public void Example()
{
#region Usage
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
using (JsonWriter writer = new JsonTextWriter(sw))
{
writer.Formatting = Formatting.Indented;
writer.WriteStartObject();
writer.WritePropertyName("CPU");
writer.WriteValue("Intel");
writer.WritePropertyName("PSU");
writer.WriteValue("500W");
writer.WritePropertyName("Drives");
writer.WriteStartArray();
writer.WriteValue("DVD read/writer");
writer.WriteComment("(broken)");
writer.WriteValue("500 gigabyte hard drive");
writer.WriteValue("200 gigabype hard drive");
writer.WriteEnd();
writer.WriteEndObject();
}
Console.WriteLine(sb.ToString());
// {
// "CPU": "Intel",
// "PSU": "500W",
// "Drives": [
// "DVD read/writer"
// /*(broken)*/,
// "500 gigabyte hard drive",
// "200 gigabype hard drive"
// ]
// }
#endregion
Assert.AreEqual(@"{
""CPU"": ""Intel"",
""PSU"": ""500W"",
""Drives"": [
""DVD read/writer""
/*(broken)*/,
""500 gigabyte hard drive"",
""200 gigabype hard drive""
]
}", sb.ToString());
}
示例2: ReadingAndWritingJsonText
public void ReadingAndWritingJsonText()
{
#region ReadingAndWritingJsonText
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
using (JsonWriter writer = new JsonTextWriter(sw))
{
writer.Formatting = Formatting.Indented;
writer.WriteStartObject();
writer.WritePropertyName("CPU");
writer.WriteValue("Intel");
writer.WritePropertyName("PSU");
writer.WriteValue("500W");
writer.WritePropertyName("Drives");
writer.WriteStartArray();
writer.WriteValue("DVD read/writer");
writer.WriteComment("(broken)");
writer.WriteValue("500 gigabyte hard drive");
writer.WriteValue("200 gigabype hard drive");
writer.WriteEnd();
writer.WriteEndObject();
}
// {
// "CPU": "Intel",
// "PSU": "500W",
// "Drives": [
// "DVD read/writer"
// /*(broken)*/,
// "500 gigabyte hard drive",
// "200 gigabype hard drive"
// ]
// }
#endregion
}
示例3: EndRequest
public override void EndRequest()
{
base.EndRequest();
Context.Response.ContentType = "application/json";
using (var streamWriter = new StreamWriter(Context.Response.OutputStream))
using (var writer = new JsonTextWriter(streamWriter))
{
writer.WriteStartObject();
writer.WritePropertyName("c");
writer.WriteValue(_channelId);
writer.WriteEndObject();
}
}
示例4: ToJson
public static string ToJson(this Person p)
{
StringWriter sw = new StringWriter();
JsonTextWriter writer = new JsonTextWriter(sw);
// {
writer.WriteStartObject();
// "name" : "Jerry"
writer.WritePropertyName("name");
writer.WriteValue(p.Name);
// "likes": ["Comedy", "Superman"]
writer.WritePropertyName("likes");
writer.WriteStartArray();
foreach (string like in p.Likes)
{
writer.WriteValue(like);
}
writer.WriteEndArray();
// }
writer.WriteEndObject();
return sw.ToString();
}
示例5: ToJsonBaseStub
/// <summary>
/// Give the base JSON information. This will be used to combine with a dataset to
/// give the complete dataset information.
///
/// To use:
/// ...
/// Write the base values
/// writer.WriteRaw(base.ToJsonBaseStub());
/// writer.WriteRaw(",");
/// ...
///
/// </summary>
/// <returns>Return a stub JSON string for the Base data.</returns>
public string ToJsonBaseStub()
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
using (JsonWriter writer = new JsonTextWriter(sw))
{
// Start the object
writer.Formatting = Formatting.None; // Make the text not indented, so not as human readable. This will save disk writing space
#region Base Values
// NumElements
writer.WritePropertyName(JSON_STR_NUMELEMENTS);
writer.WriteValue(NumElements);
// ElementsMultiplier
writer.WritePropertyName(JSON_STR_ELEMENTSMULTIPLIER);
writer.WriteValue(ElementsMultiplier);
#endregion
}
return sb.ToString();
}
示例6: SerializeMessage
public static void SerializeMessage(JsonTextWriter writer, ServiceType type, object message)
{
Require.NotNull(writer, "writer");
Require.NotNull(type, "type");
Require.NotNull(message, "message");
writer.WriteStartObject();
foreach (var field in type.Fields.Values)
{
if (
field.ShouldSerializeMethod != null &&
!field.ShouldSerializeMethod(message)
)
continue;
writer.WritePropertyName(field.Tag.ToString(CultureInfo.InvariantCulture));
object value = field.Getter(message);
if (value == null)
{
writer.WriteNull();
}
else if (field.CollectionType != null)
{
writer.WriteStartArray();
foreach (object item in (IEnumerable)value)
{
if (field.ServiceType != null && !field.ServiceType.Type.IsEnum)
SerializeMessage(writer, field.ServiceType, item);
else
writer.WriteValue(item);
}
writer.WriteEndArray();
}
else
{
if (field.ServiceType != null && !field.ServiceType.Type.IsEnum)
SerializeMessage(writer, field.ServiceType, value);
else
writer.WriteValue(value);
}
}
writer.WriteEndObject();
}
示例7: BuildResponseJson
private void BuildResponseJson(StringBuilder sb, PendingDownstreamMessage message)
{
using (var stringWriter = new StringWriter(sb))
using (var writer = new JsonTextWriter(stringWriter))
{
if (message == null)
{
writer.WriteStartArray();
writer.WriteValue("noop");
writer.WriteEndArray();
}
else
{
var responseType = message.Message.GetType();
var responseServiceType = ServiceRegistry.GetAssembly(responseType.Assembly).TypesByType[responseType];
writer.WriteStartObject();
writer.WritePropertyName("r");
writer.WriteValue((int)message.Kind);
writer.WritePropertyName("a");
writer.WriteValue((int)message.AssociationId);
writer.WritePropertyName("t");
writer.WriteValue(responseServiceType.Message.Id);
writer.WritePropertyName("p");
JsonUtil.SerializeMessage(writer, responseServiceType, message.Message);
writer.WriteEndObject();
}
}
}