从WebApi到Asp.net格式化JSON数据[服务器端]

时间:2017-01-12 05:26:45

标签: asp.net json json-deserialization

 [HttpGet]
        public ActionResult Sync()
        {

            string HApi = "demo";
            string appId = "";
            string company;
            string firstname;
            string lastmodifieddate;
            string lastname;



            string url = string.Format("https://api.hubapi.com/contacts/v1/lists/all/contacts/all?hapikey={0}", HApi);
            using (WebClient client = new WebClient())
            {
                string json = client.DownloadString(url);
                LiveReportSync liveSync = (new JavaScriptSerializer()).Deserialize<LiveReportSync>(json);

// From this doesnt work 
                firstname = liveSync.firstname.ToString();
                lastmodifieddate = liveSync.lastmodifieddate.ToString();
                company = liveSync.company.ToString();
                lastname = liveSync.lastname.ToString();
            }

            return View();
        }

现在输出

{"contacts":[{"addedAt":1390574181854,"vid":204727,"canonical-vid":204727,"merged-vids":[],"portal-id":62515,"is-contact":true,"profile-token":"AO_T-mPqtf_Gb7kIE3oMO0t7laL9CUb8gro7gEaIu7dpzETGI5Qate4csN1pv7LLwKX0T-EBZr-rNZpsdonxI36ppzWJIWxryUrWGrevbHLSfyD4QzNgRGg8AOnFodXPBDlR9kykxIPL","profile-url":"https://app.hubspot.com/contacts/62515/lists/public/contact/_AO_T-mPqtf_Gb7kIE3oMO0t7laL9CUb8gro7gEaIu7dpzETGI5Qate4csN1pv7LLwKX0T-EBZr-rNZpsdonxI36ppzWJIWxryUrWGrevbHLSfyD4QzNgRGg8AOnFodXPBDlR9kykxIPL/","properties":{"firstname":{"value":"Bob"},"lastmodifieddate":{"value":"1483461406481"},"company":{"value":""},"lastname":{"value":"Record"}},"form-submissions":[],"identity-profiles":[{"vid":204727,"saved-at-timestamp":1476768116149,"deleted-changed-timestamp":0,"identities":[{"type":"LEAD_GUID","value":"f9d728f1-dff1-49b0-9caa-247dbdf5b8b7","timestamp":1390574181878},{"type":"EMAIL","value":"mgnew-email@hubspot.com","timestamp":1476768116137}]}],"merge-audits":[]},{"addedAt":1392643921079,"vid":207303,"canonical-vid":207303,"merged-vids":[],"portal-id":62515,"is-contact":true,"profile-token":"AO_T-mNzaLWtoil0-THRN0tSOkWGwHqG4wH1-CAzOaSyuomoFAgd1RAxfWyaNwvoaTY7wpYXEozId-HnIzzaDFHoMr4AMtW4Cj4BG0Ku9KSxxFOkcROdXJ_-lqAAhUvYJidoK_oUnGrS","profile-url":"https://app.hubspot.com/contacts/62515/lists/public/contact/_AO_T-mNzaLWtoil0-THRN0tSOkWGwHqG4wH1-CAzOaSyuomoFAgd1RAxfWyaNwvoaTY7wpYXEozId-HnIzzaDFHoMr4AMtW4Cj4BG0Ku9KSxxFOkcROdXJ_-lqAAhUvYJidoK_oUnGrS/","properties":{"firstname":{"value":"Ff_FirstName_0"},"lastmodifieddate":{"value":"1479148429488"},"lastname":{"value":"Ff_LastName_0"}},"form-submissions":[],"identity-profiles":[{"vid":207303,"saved-at-timestamp":1392643921090,"deleted-changed-timestamp":0,"identities":[{"type":"EMAIL","value":"email_0be34aebe5@abctest.com","timestamp":1392643921079},{"type":"LEAD_GUID","value":"058378c6-9513-43e1-a13a-43a98d47aa22","timestamp":1392643921082}]}],"merge-audits":[]},{"addedAt":1392646026523,"vid":207323,"canonical-vid":207323,"merged-vids":[],"portal-id":62515,"is-contact":true,"profile-token":"AO_T-mPYdmrPnVlj7ZWVYMITedAigswmj8Go9yY50CcP7sMjT19Oatzv64sXDDz4YWMu5kXx8jcrzUz34dTkY3FdEOF50goj0duVIYZxfjeCWwB1BbN2QFjEFe4HUClVXrDaWMYqZ2BO","profile-url":"https://app.hubspot.com/contacts/62515/lists/public/contact/_AO_T-mPYdmrPnVlj7ZWVYMITedAigswmj8Go9yY50CcP7sMjT19Oatzv64sXDDz4YWMu5kXx8jcrzUz34dTkY3FdEOF50goj0duVIYZxfjeCWwB1BbN2QFjEFe4HUClVXrDaWMYqZ2BO/","properties":{"firstname":{"value":"Ff_FirstName_3"},"lastmodifieddate":{"value":"1479148429310"},"company":{"value":""},"lastname":{"value":"Ff_LastName_3"}}....................

我需要属性的值,即Firstname LastName Company等 还需要为每个循环输入一个不同的Sql行中的每个第一个

1 个答案:

答案 0 :(得分:0)

请使类文件映射您的json数据,如下所示(http://json2csharp.com/)将帮助您从json数据生成类。

 public class Firstname
{
    public string value { get; set; }
}

public class Lastmodifieddate
{
    public string value { get; set; }
}

public class Company
{
    public string value { get; set; }
}

public class Lastname
{
    public string value { get; set; }
}

public class Properties
{
    public Firstname firstname { get; set; }
    public Lastmodifieddate lastmodifieddate { get; set; }
    public Company company { get; set; }
    public Lastname lastname { get; set; }
}

public class Identity
{
    public string type { get; set; }
    public string value { get; set; }
    public object timestamp { get; set; }
}

public class IdentityProfile
{
    public int vid { get; set; }
    public object __invalid_name__saved-at-timestamp { get; set; }
    public int __invalid_name__deleted-changed-timestamp { get; set; }
    public List<Identity> identities { get; set; }
}

public class Contact
{
    public object addedAt { get; set; }
    public int vid { get; set; }
    public int __invalid_name__canonical-vid { get; set; }
    public List<object> __invalid_name__merged-vids { get; set; }
    public int __invalid_name__portal-id { get; set; }
    public bool __invalid_name__is-contact { get; set; }
    public string __invalid_name__profile-token { get; set; }
    public string __invalid_name__profile-url { get; set; }
    public Properties properties { get; set; }
    public List<object> __invalid_name__form-submissions { get; set; }
    public List<IdentityProfile> __invalid_name__identity-profiles { get; set; }
    public List<object> __invalid_name__merge-audits { get; set; }
}

public class RootObject
{
    public List<Contact> contacts { get; set; }
    public bool __invalid_name__has-more { get; set; }
    public int __invalid_name__vid-offset { get; set; }
}

将json反序列化为类对象

 result = JObject.Parse(json).SelectToken("json").ToObject<RootObject>();