using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LYFZ.Weixin.SDK
{
///
/// 模板
///
public class Template
{
///
/// 模板id
///
public string TemplateId { get; set; }
///
/// 标题
///
public string Title { get; set; }
///
/// 一级行业
///
public string PrimaryIndustry { get; set; }
///
/// 二级行业
///
public string DeputyIndustry { get; set; }
///
/// 内容
///
public string Content { get; set; }
///
/// 示例
///
public string Example { get; set; }
///
/// 构造函数
///
/// 模板id
/// 标题
/// 一级行业
/// 二级行业
/// 内容
/// 示例
public Template(string templateId, string title, string primaryIndustry, string deputyIndustry,
string content, string example)
{
TemplateId = templateId;
Title = title;
PrimaryIndustry = primaryIndustry;
DeputyIndustry = deputyIndustry;
Content = content;
Example = example;
}
///
/// 构造函数
///
public Template()
{ }
///
/// 返回字符串
///
///
public override string ToString()
{
return string.Format("模板id:{0}\r\n标题:{1}\r\n一级行业:{2}\r\n二级行业:{3}\r\n" +
"内容:{4}\r\n示例:{5}",
TemplateId, Title, PrimaryIndustry, DeputyIndustry,
Content, Example);
}
///
/// 从JObject对象中解析统计数据
///
///
public void Parse(Newtonsoft.Json.Linq.JObject jo)
{
TemplateId = (string)jo["template_id"];
Title = (string)jo["title"];
PrimaryIndustry = (string)jo["primary_industry"];
DeputyIndustry = (string)jo["deputy_industry"];
Content = (string)jo["content"];
Example = (string)jo["example"];
}
///
/// 解析模板
///
/// JSON格式的模板数据
/// 返回模板数组
public static ErrorMessage Parse(string json, out Template[] templates)
{
templates = null;
ErrorMessage errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "");
if (string.IsNullOrWhiteSpace(json))
{
errorMessage.errmsg = "请求失败。";
}
else if (ErrorMessage.IsErrorMessage(json))
{
errorMessage = ErrorMessage.Parse(json);
}
else
{
JObject jo = JObject.Parse(json);
JToken jt;
if (jo.TryGetValue("template_list", out jt) && jt.Type == JTokenType.Array)
{
errorMessage = new ErrorMessage(ErrorMessage.SuccessCode, "请求成功。");
JArray ja = (JArray)jt;
templates = new Template[ja.Count];
for (int i = 0; i < ja.Count; i++)
{
templates[i] = new Template();
templates[i].Parse((JObject)ja[i]);
}
}
else
{
errorMessage.errmsg = "解析结果失败。";
}
}
return errorMessage;
}
}
}