我开发了json feed
我期待输出
{
"SectionId": "3",
"SectionName": "News",
"ParentSectionId": "",
"MetaTitle": "News",
"MetaDescription": "News",
"MetaKeyword": "News",
"Status": "A",
"URL": "",
"SiteID": "1",
"RegionalLanguage1": "News",
"RegionalLanguage2": "",
"CreatedBy": "20",
"CreatedDate": "11-11-2013 12:48:28",
"IsSearchable": "Y",
"LastUpdatedBy": "20",
"LastUpdatedDate": "11-11-2013 12:54:51",
"IsRSS": "True",
"RssOrder": "",
"ISCMS": "1",
"Priority": "",
"IsVideo": "0",
"IsDisplayMenu": "0",
"phone": [
{
"SectionId": "7",
"SectionName": "Entertainment",
"ParentSectionId": "",
"MetaTitle": "Entertainment",
"MetaDescription": "Entertainment",
"MetaKeyword": "Entertainment",
"Status": "A",
"URL": "",
"SiteID": "1",
"RegionalLanguage1": "Entertainment",
"RegionalLanguage2": "",
"CreatedBy": "20",
"CreatedDate": "11-11-2013 12:50:50",
"IsSearchable": "Y",
"LastUpdatedBy": "20",
"LastUpdatedDate": "17-12-2013 13:49:15",
"IsRSS": "True",
"RssOrder": "",
"ISCMS": "0",
"Priority": "",
"IsVideo": "0",
"IsDisplayMenu": "1",
},
]
},
使用以下代码
var cont = new Cont();
var pho1=new Phone();
JavaScriptSerializer serializer = new JavaScriptSerializer();
var objectToSerialize = new RootObject();
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());
SqlCommand command = new SqlCommand("select TOP 1000 [SectionId] ,[SectionName] ,[ParentSectionId] ,[MetaTitle] ,[MetaDescription] ,[MetaKeyword] ,[Status],[URL] ,[SiteID] ,[RegionalLanguage1],[RegionalLanguage2] ,[CreatedBy] ,[CreatedDate],[IsSearchable],[LastUpdatedBy],[LastUpdatedDate],[IsRSS],[RssOrder],[ISCMS],[Priority],[IsVideo],[IsDisplayMenu] FROM [DATABASE1].[dbo].[EDMstSection] where ParentSectionId is null", connection);
SqlDataAdapter da = new SqlDataAdapter(command);
da.Fill(dt);
List<Cont> ContCollection = new List<Cont>();
foreach (DataRow dr in dt.Rows)
{
cont = new Cont()
{
SectionId = dr["SectionId"].ToString(),
SectionName = dr["SectionName"].ToString(),
ParentSectionId = dr["ParentSectionId"].ToString(),
MetaTitle = dr["MetaTitle"].ToString(),
MetaDescription = dr["MetaDescription"].ToString(),
MetaKeyword = dr["MetaKeyword"].ToString(),
Status = dr["Status"].ToString(),
URL = dr["URL"].ToString(),
SiteID = dr["SiteID"].ToString(),
RegionalLanguage1 = dr["RegionalLanguage1"].ToString(),
RegionalLanguage2 = dr["RegionalLanguage2"].ToString(),
CreatedBy = dr["CreatedBy"].ToString(),
CreatedDate = dr["CreatedDate"].ToString(),
IsSearchable = dr["IsSearchable"].ToString(),
LastUpdatedBy = dr["LastUpdatedBy"].ToString(),
LastUpdatedDate = dr["LastUpdatedDate"].ToString(),
IsRSS = dr["IsRSS"].ToString(),
RssOrder = dr["RssOrder"].ToString(),
ISCMS = dr["ISCMS"].ToString(),
Priority = dr["Priority"].ToString(),
IsVideo = dr["IsVideo"].ToString(),
IsDisplayMenu = dr["IsDisplayMenu"].ToString(),
// phone = new Phone { Section = "kgljh" }
// phone = new List<Phone>()
};
ContCollection.Add(cont);
cont.phone = new List<Phone>();
DataTable dt1 = new DataTable();
SqlCommand command1 = new SqlCommand("SELECT TOP 1000 [SectionId] ,[SectionName] ,[ParentSectionId] ,[MetaTitle] ,[MetaDescription] ,[MetaKeyword] ,[Status],[URL] ,[SiteID] ,[RegionalLanguage1],[RegionalLanguage2] ,[CreatedBy] ,[CreatedDate],[IsSearchable],[LastUpdatedBy],[LastUpdatedDate],[IsRSS],[RssOrder],[ISCMS],[Priority],[IsVideo],[IsDisplayMenu] FROM [DATABASE1].[dbo].[EDMstSection] where ParentSectionId='" + cont.ParentSectionId + "'", connection);
SqlDataAdapter sqladapter = new SqlDataAdapter(command1);
sqladapter.Fill(dt1);
foreach (DataRow dr1 in dt1.Rows)
{
pho1 = new Phone
{
SectionId = dr1["SectionId"].ToString(),
SectionName = dr1["SectionName"].ToString(),
ParentSectionId = dr1["ParentSectionId"].ToString(),
MetaTitle = dr1["MetaTitle"].ToString(),
MetaDescription = dr1["MetaDescription"].ToString(),
MetaKeyword = dr1["MetaKeyword"].ToString(),
Status = dr1["Status"].ToString(),
URL = dr1["URL"].ToString(),
SiteID = dr1["SiteID"].ToString(),
RegionalLanguage1 = dr1["RegionalLanguage1"].ToString(),
RegionalLanguage2 = dr1["RegionalLanguage2"].ToString(),
CreatedBy = dr1["CreatedBy"].ToString(),
CreatedDate = dr1["CreatedDate"].ToString(),
IsSearchable = dr1["IsSearchable"].ToString(),
LastUpdatedBy = dr1["LastUpdatedBy"].ToString(),
LastUpdatedDate = dr1["LastUpdatedDate"].ToString(),
IsRSS = dr1["IsRSS"].ToString(),
RssOrder = dr1["RssOrder"].ToString(),
ISCMS = dr1["ISCMS"].ToString(),
Priority = dr1["Priority"].ToString(),
IsVideo = dr1["IsVideo"].ToString(),
IsDisplayMenu = dr1["IsDisplayMenu"].ToString(),
};
cont.phone.Add(pho1);
}
}
ContCollection.Add(cont);
objectToSerialize.contacts = ContCollection;
Response.Write(serializer.Serialize(new { item = objectToSerialize }));
我的数据库新闻是主要部分,新闻包含四个子部分。
但我输出的是新闻和娱乐是主要部分。
我需要娱乐是新闻的子部分
我得到了以下输出
{
"SectionId": "3",
"SectionName": "News",
"ParentSectionId": "",
"MetaTitle": "News",
"MetaDescription": "News",
"MetaKeyword": "News",
"Status": "A",
"URL": "",
"SiteID": "1",
"RegionalLanguage1": "News",
"RegionalLanguage2": "",
"CreatedBy": "20",
"CreatedDate": "11-11-2013 12:48:28",
"IsSearchable": "Y",
"LastUpdatedBy": "20",
"LastUpdatedDate": "11-11-2013 12:54:51",
"IsRSS": "True",
"RssOrder": "",
"ISCMS": "1",
"Priority": "",
"IsVideo": "0",
"IsDisplayMenu": "0",
"phone": []
},
{
"SectionId": "7",
"SectionName": "Entertainment",
"ParentSectionId": "",
"MetaTitle": "Entertainment",
"MetaDescription": "Entertainment",
"MetaKeyword": "Entertainment",
"Status": "A",
"URL": "",
"SiteID": "1",
"RegionalLanguage1": "Entertainment",
"RegionalLanguage2": "",
"CreatedBy": "20",
"CreatedDate": "11-11-2013 12:50:50",
"IsSearchable": "Y",
"LastUpdatedBy": "20",
"LastUpdatedDate": "17-12-2013 13:49:15",
"IsRSS": "True",
"RssOrder": "",
"ISCMS": "0",
"Priority": "",
"IsVideo": "0",
"IsDisplayMenu": "1",
"phone": []
},
任何人都会帮我解决我的代码中的错误。
答案 0 :(得分:1)
使用Json.NET非常简单:
dynamic stuff = JsonConvert.DeserializeObject("{ 'Name': 'Jon Smith', 'Address': { 'City': 'New York', 'State': 'NY' }, 'Age': 42 }");
string name = stuff.Name;
string address = stuff.Address.City;
答案 1 :(得分:0)
根据你需要的json对象,你可以拥有这两个模型类:
public class JsonWrapper
{
public string SectionId { get; set; }
public string SectionName { get; set; }
public string ParentSectionId { get; set; }
public string MetaTitle { get; set; }
public string MetaDescription { get; set; }
public string MetaKeyword { get; set; }
public string Status { get; set; }
public string URL { get; set; }
public string SiteID { get; set; }
public string RegionalLanguage1 { get; set; }
public string RegionalLanguage2 { get; set; }
public string CreatedBy { get; set; }
public string CreatedDate { get; set; }
public string IsSearchable { get; set; }
public string LastUpdatedBy { get; set; }
public string LastUpdatedDate { get; set; }
public string IsRSS { get; set; }
public string RssOrder { get; set; }
public string ISCMS { get; set; }
public string Priority { get; set; }
public string IsVideo { get; set; }
public string IsDisplayMenu { get; set; }
public List<Phone> phone { get; set; }
}
public class Phone
{
public string SectionId { get; set; }
public string SectionName { get; set; }
public string ParentSectionId { get; set; }
public string MetaTitle { get; set; }
public string MetaDescription { get; set; }
public string MetaKeyword { get; set; }
public string Status { get; set; }
public string URL { get; set; }
public string SiteID { get; set; }
public string RegionalLanguage1 { get; set; }
public string RegionalLanguage2 { get; set; }
public string CreatedBy { get; set; }
public string CreatedDate { get; set; }
public string IsSearchable { get; set; }
public string LastUpdatedBy { get; set; }
public string LastUpdatedDate { get; set; }
public string IsRSS { get; set; }
public string RssOrder { get; set; }
public string ISCMS { get; set; }
public string Priority { get; set; }
public string IsVideo { get; set; }
public string IsDisplayMenu { get; set; }
}
您可以将数据读入这两个类,然后使用Newtonsoft库将其序列化为json字符串。