[C#] 纯文本查看 复制代码 using System;
using JsonCSharpClassGenerator;
namespace SufeiNet
{
public class SufeiNet_Test
{
public SufeiNet_Test(string json)
: this(JObject.Parse(json))
{
}
private JObject __jobject;
public SufeiNet_Test(JObject obj)
{
this.__jobject = obj;
}
public object u
{
get
{
return JsonClassHelper.ReadObject(JsonClassHelper.GetJToken<JToken>(__jobject, "u"));
}
}
[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
private object[][] _l;
public object[][] l
{
get
{
if (_l == null)
_l = (object[][])JsonClassHelper.ReadArray<object>(JsonClassHelper.GetJToken<JArray>(__jobject, "l"), JsonClassHelper.ReadObject, typeof(object[][]));
return _l;
}
}
[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
private object[][] _n;
public object[][] n
{
get
{
if (_n == null)
_n = (object[][])JsonClassHelper.ReadArray<object>(JsonClassHelper.GetJToken<JArray>(__jobject, "n"), JsonClassHelper.ReadObject, typeof(object[][]));
return _n;
}
}
public object e
{
get
{
return JsonClassHelper.ReadObject(JsonClassHelper.GetJToken<JToken>(__jobject, "e"));
}
}
public int lg
{
get
{
return JsonClassHelper.ReadInteger(JsonClassHelper.GetJToken<JValue>(__jobject, "lg"));
}
}
public int pt
{
get
{
return JsonClassHelper.ReadInteger(JsonClassHelper.GetJToken<JValue>(__jobject, "pt"));
}
}
public object ps
{
get
{
return JsonClassHelper.ReadObject(JsonClassHelper.GetJToken<JToken>(__jobject, "ps"));
}
}
public object d
{
get
{
return JsonClassHelper.ReadObject(JsonClassHelper.GetJToken<JToken>(__jobject, "d"));
}
}
}
}
// JSON C# Class Generator
// [url]http://www.sufeinet.com/[/url]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
namespace JsonCSharpClassGenerator
{
internal static class JsonClassHelper
{
public static T GetJToken<T>(JObject obj, string field) where T : JToken
{
JToken value;
if (obj.TryGetValue(field, out value)) return GetJToken<T>(value);
else return null;
}
private static T GetJToken<T>(JToken token) where T : JToken
{
if (token == null) return null;
if (token.Type == JTokenType.Null) return null;
if (token.Type == JTokenType.Undefined) return null;
return (T)token;
}
public static string ReadString(JToken token)
{
var value = GetJToken<JValue>(token);
if (value == null) return null;
return (string)value.Value;
}
public static bool ReadBoolean(JToken token)
{
var value = GetJToken<JValue>(token);
if (value == null) throw new Newtonsoft.Json.JsonSerializationException();
return Convert.ToBoolean(value.Value);
}
public static bool? ReadNullableBoolean(JToken token)
{
var value = GetJToken<JValue>(token);
if (value == null) return null;
return Convert.ToBoolean(value.Value);
}
public static int ReadInteger(JToken token)
{
var value = GetJToken<JValue>(token);
if (value == null) throw new Newtonsoft.Json.JsonSerializationException();
return Convert.ToInt32((long)value.Value);
}
public static int? ReadNullableInteger(JToken token)
{
var value = GetJToken<JValue>(token);
if (value == null) return null;
return Convert.ToInt32((long)value.Value);
}
public static long ReadLong(JToken token)
{
var value = GetJToken<JValue>(token);
if (value == null) throw new Newtonsoft.Json.JsonSerializationException();
return Convert.ToInt64(value.Value);
}
public static long? ReadNullableLong(JToken token)
{
var value = GetJToken<JValue>(token);
if (value == null) return null;
return Convert.ToInt64(value.Value);
}
public static double ReadFloat(JToken token)
{
var value = GetJToken<JValue>(token);
if (value == null) throw new Newtonsoft.Json.JsonSerializationException();
return Convert.ToDouble(value.Value);
}
public static double? ReadNullableFloat(JToken token)
{
var value = GetJToken<JValue>(token);
if (value == null) return null;
return Convert.ToDouble(value.Value);
}
public static DateTime ReadDate(JToken token)
{
var value = GetJToken<JValue>(token);
if (value == null) throw new Newtonsoft.Json.JsonSerializationException();
return Convert.ToDateTime(value.Value);
}
public static DateTime? ReadNullableDate(JToken token)
{
var value = GetJToken<JValue>(token);
if (value == null) return null;
return Convert.ToDateTime(value.Value);
}
public static object ReadObject(JToken token)
{
var value = GetJToken<JToken>(token);
if (value == null) return null;
if (value.Type == JTokenType.Object) return value;
if (value.Type == JTokenType.Array) return ReadArray<object>(value, ReadObject);
var jvalue = value as JValue;
if (jvalue != null) return jvalue.Value;
return value;
}
public static T ReadStronglyTypedObject<T>(JToken token) where T : class
{
var value = GetJToken<JObject>(token);
if (value == null) return null;
return (T)Activator.CreateInstance(typeof(T), new object[] { token });
}
public delegate T ValueReader<T>(JToken token);
public static T[] ReadArray<T>(JToken token, ValueReader<T> reader)
{
var value = GetJToken<JArray>(token);
if (value == null) return null;
var array = new T[value.Count];
for (int i = 0; i < array.Length; i++)
{
array[i] = reader(value[i]);
}
return array;
}
public static Dictionary<string, T> ReadDictionary<T>(JToken token)
{
var value = GetJToken<JObject>(token);
if (value == null) return null;
var dict = new Dictionary<string, T>();
return dict;
}
public static Array ReadArray<K>(JArray jArray, ValueReader<K> reader, Type type)
{
if (jArray == null) return null;
var elemType = type.GetElementType();
var array = Array.CreateInstance(elemType, jArray.Count);
for (int i = 0; i < array.Length; i++)
{
if (elemType.IsArray)
{
array.SetValue(ReadArray<K>(GetJToken<JArray>(jArray[i]), reader, elemType), i);
}
else
{
array.SetValue(reader(jArray[i]), i);
}
}
return array;
}
}
}
我想问下转换C#实体类,那么网站接口每天的JSON数据都不一样,不就是要每天都要在C#实体类里修改后编译一次程序才能访问该网站接口的JSON数据?能否让程序接收JSON每天不一样的数据? |