using System;
using System.Collections.Generic;
using System.Text;
namespace Pixysoft.Json
{
public class CommonJsonModel : CommonJsonModelAnalyzer
{
private string rawjson;
private bool isValue = false;
private bool isModel = false;
private bool isCollection = false;
internal CommonJsonModel(string rawjson)
{
this.rawjson = rawjson;
if (string.IsNullOrEmpty(rawjson))
throw new Exception("missing rawjson");
rawjson = rawjson.Trim();
if (rawjson.StartsWith("{"))
{
isModel = true;
}
else if (rawjson.StartsWith("["))
{
isCollection = true;
}
else
{
isValue = true;
}
}
public string Rawjson
{
get { return rawjson; }
}
public bool IsValue()
{
return isValue;
}
public bool IsValue(string key)
{
if (!isModel)
return false;
if (string.IsNullOrEmpty(key))
return false;
foreach (string subjson in base._GetCollection(this.rawjson))
{
CommonJsonModel model = new CommonJsonModel(subjson);
if (!model.IsValue())
continue;
if (model.Key == key)
{
CommonJsonModel submodel = new CommonJsonModel(model.Value);
return submodel.IsValue();
}
}
return false;
}
public bool IsModel()
{
return isModel;
}
public bool IsModel(string key)
{
if (!isModel)
return false;
if (string.IsNullOrEmpty(key))
return false;
foreach (string subjson in base._GetCollection(this.rawjson))
{
CommonJsonModel model = new CommonJsonModel(subjson);
if (!model.IsValue())
continue;
if (model.Key == key)
{
CommonJsonModel submodel = new CommonJsonModel(model.Value);
return submodel.IsModel();
}
}
return false;
}
public bool IsCollection()
{
return isCollection;
}
public bool IsCollection(string key)
{
if (!isModel)
return false;
if (string.IsNullOrEmpty(key))
return false;
foreach (string subjson in base._GetCollection(this.rawjson))
{
CommonJsonModel model = new CommonJsonModel(subjson);
if (!model.IsValue())
continue;
if (model.Key == key)
{
CommonJsonModel submodel = new CommonJsonModel(model.Value);
return submodel.IsCollection();
}
}
return false;
}
/// summary>
/// 當模型是對象,返回擁有的key
/// /summary>
/// returns>/returns>
public Liststring> GetKeys()
{
if (!isModel)
return null;
Liststring> list = new Liststring>();
foreach (string subjson in base._GetCollection(this.rawjson))
{
string key = new CommonJsonModel(subjson).Key;
if (!string.IsNullOrEmpty(key))
list.Add(key);
}
return list;
}
/// summary>
/// 當模型是對象,key對應是值,則返回key對應的值
/// /summary>
/// param name="key">/param>
/// returns>/returns>
public string GetValue(string key)
{
if (!isModel)
return null;
if (string.IsNullOrEmpty(key))
return null;
foreach (string subjson in base._GetCollection(this.rawjson))
{
CommonJsonModel model = new CommonJsonModel(subjson);
if (!model.IsValue())
continue;
if (model.Key == key)
return model.Value;
}
return null;
}
/// summary>
/// 模型是對象,key對應是對象,返回key對應的對象
/// /summary>
/// param name="key">/param>
/// returns>/returns>
public CommonJsonModel GetModel(string key)
{
if (!isModel)
return null;
if (string.IsNullOrEmpty(key))
return null;
foreach (string subjson in base._GetCollection(this.rawjson))
{
CommonJsonModel model = new CommonJsonModel(subjson);
if (!model.IsValue())
continue;
if (model.Key == key)
{
CommonJsonModel submodel = new CommonJsonModel(model.Value);
if (!submodel.IsModel())
return null;
else
return submodel;
}
}
return null;
}
/// summary>
/// 模型是對象,key對應是集合,返回集合
/// /summary>
/// param name="key">/param>
/// returns>/returns>
public CommonJsonModel GetCollection(string key)
{
if (!isModel)
return null;
if (string.IsNullOrEmpty(key))
return null;
foreach (string subjson in base._GetCollection(this.rawjson))
{
CommonJsonModel model = new CommonJsonModel(subjson);
if (!model.IsValue())
continue;
if (model.Key == key)
{
CommonJsonModel submodel = new CommonJsonModel(model.Value);
if (!submodel.IsCollection())
return null;
else
return submodel;
}
}
return null;
}
/// summary>
/// 模型是集合,返回自身
/// /summary>
/// returns>/returns>
public ListCommonJsonModel> GetCollection()
{
ListCommonJsonModel> list = new ListCommonJsonModel>();
if (IsValue())
return list;
foreach (string subjson in base._GetCollection(rawjson))
{
list.Add(new CommonJsonModel(subjson));
}
return list;
}
/// summary>
/// 當模型是值對象,返回key
/// /summary>
private string Key
{
get
{
if (IsValue())
return base._GetKey(rawjson);
return null;
}
}
/// summary>
/// 當模型是值對象,返回value
/// /summary>
private string Value
{
get
{
if (!IsValue())
return null;
return base._GetValue(rawjson);
}
}
}
}
1. 首先377149574代表了一個集合,所以要用model.GetCollection("377149574")把這個集合取出來。
2. 其次這個集合里面包含了很多對象,因此用GetColllection()把這些對象取出來
3. 在這些對象List里面取第一個[0],表示取了":{"cid":"377149574"}這個對象,然后再用GetValue("cid")把對象的值取出來。