本文整理汇总了C#中JToken.AsString方法的典型用法代码示例。如果您正苦于以下问题:C# JToken.AsString方法的具体用法?C# JToken.AsString怎么用?C# JToken.AsString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JToken
的用法示例。
在下文中一共展示了JToken.AsString方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ParseDateTimeOffset
public static DateTimeOffset? ParseDateTimeOffset(JToken value)
{
var jValue = value as JValue;
if (jValue != null)
{
if (jValue.Value == null)
return null;
if (jValue.Value is DateTimeOffset)
return jValue.Value<DateTimeOffset>();
}
var rawValue = value.AsString();
if (string.IsNullOrWhiteSpace(rawValue))
return null;
rawValue = rawValue.Replace("NeoDate", "Date");
if (!DateRegex.IsMatch(rawValue))
{
DateTimeOffset parsed;
if (!DateTimeOffset.TryParse(rawValue, out parsed))
return null;
}
var text = string.Format("{{\"a\":\"{0}\"}}", rawValue);
var reader = new JsonTextReader(new StringReader(text)) {DateParseHandling = DateParseHandling.DateTimeOffset};
reader.Read(); // JsonToken.StartObject
reader.Read(); // JsonToken.PropertyName
return reader.ReadAsDateTimeOffset();
}
示例2: ConvertFromJson
public object ConvertFromJson(JToken value)
{
string text = value.AsString();
return text != null ?
new Uri(text) :
null;
}
示例3: ParseDateTime
public static DateTime? ParseDateTime(JToken value)
{
var rawValue = value.AsString();
if (string.IsNullOrWhiteSpace(rawValue))
return null;
rawValue = rawValue.Replace("NeoDate", "Date");
if (!DateRegex.IsMatch(rawValue))
{
DateTime parsed;
if (!DateTime.TryParse(rawValue, out parsed))
return null;
return rawValue.EndsWith("Z", StringComparison.InvariantCultureIgnoreCase) ? parsed.ToUniversalTime() : parsed;
}
var text = string.Format("{{\"a\":\"{0}\"}}", rawValue);
var reader = new JsonTextReader(new StringReader(text));
reader.Read(); // JsonToken.StartObject
reader.Read(); // JsonToken.PropertyName
return reader.ReadAsDateTime();
}
示例4: CoerceValue
public static object CoerceValue(DeserializationContext context, PropertyInfo propertyInfo, JToken value, IEnumerable<TypeMapping> typeMappings, int nestingLevel)
{
if (value == null || value.Type == JTokenType.Null)
return null;
var propertyType = propertyInfo.PropertyType;
object jsonConversionResult;
if (TryJsonConverters(context, propertyType, value, out jsonConversionResult))
return jsonConversionResult;
Type genericTypeDef = null;
if (propertyType.IsGenericType)
{
genericTypeDef = propertyType.GetGenericTypeDefinition();
if (genericTypeDef == typeof(Nullable<>))
{
propertyType = propertyType.GetGenericArguments()[0];
genericTypeDef = null;
}
}
typeMappings = typeMappings.ToArray();
if (propertyType.IsPrimitive)
{
// no primitives can contain quotes so we can safely remove them
// allows converting a json value like {"index": "1"} to an int
object tmpVal = value.AsString().Replace("\"", string.Empty);
tmpVal = Convert.ChangeType(tmpVal, propertyType);
return tmpVal;
}
if (propertyType.IsEnum)
{
var raw = value.AsString();
var converted = Enum.Parse(propertyType, raw, false);
return converted;
}
if (propertyType == typeof(Uri))
{
var raw = value.AsString();
var uri = new Uri(raw, UriKind.RelativeOrAbsolute);
return uri;
}
if (propertyType == typeof(string))
{
var raw = value.AsString();
return raw;
}
if (propertyType == typeof(DateTime))
{
return ParseDateTime(value);
}
if (propertyType == typeof(DateTimeOffset))
{
var dateTimeOffset = ParseDateTimeOffset(value);
if (dateTimeOffset.HasValue)
return dateTimeOffset.Value;
return null;
}
if (propertyType == typeof(Decimal))
{
var dec = Convert.ToDecimal(((JValue) value).Value);
return dec;
}
if (propertyType == typeof(TimeSpan))
{
var valueString = value.ToString();
var timeSpan = TimeSpan.Parse(valueString);
return timeSpan;
}
if (propertyType == typeof(Guid))
{
var raw = value.AsString();
var guid = string.IsNullOrEmpty(raw) ? Guid.Empty : new Guid(raw);
return guid;
}
if (propertyType == typeof(byte[]))
{
return Convert.FromBase64String(value.Value<string>());
}
if (genericTypeDef == typeof(List<>))
{
var list = BuildList(context, propertyType, value.Children(), typeMappings, nestingLevel + 1);
return list;
}
if (genericTypeDef == typeof(Dictionary<,>))
{
var keyType = propertyType.GetGenericArguments()[0];
//.........这里部分代码省略.........