2012-05-18 82 views
0

a .net服務給我這個來自twitter,我只需要大約五個字段,我已經做了JSON給出,這只是JSON對象的數組可以是相當大的,似乎沒有必要爲客戶端提供一個較大的JSON文件與一個較小的JSON文件,例如,目的只包括一個JSON中的對象,可能非常多..問題是如何在js文件中提供它之前將其縮小,所以解析在一些後端的方式,所以只提供我需要的屬性。收縮/剝離給定的JSON對象

var twitter_peeps = { 
twitterFeedData: 
[{ 
    "in_reply_to_user_id_str":null, 
    "truncated":false, 
    "contributors":null, 
    "created_at":"Fri May 18 19:20:34 +0000 2012", 
    "in_reply_to_screen_name":null, 
    "favorited":false, 
    "geo":null, 
    "user":{ 
     "id":16819632, 
     "verified":false, 
     "default_profile":true, 
     "profile_background_color":"C0DEED", 
     "statuses_count":8595, 
     "following":true, 
     "time_zone":"Central Time (US & Canada)", 
     "profile_background_image_url":"http:\/\/a0.twimg.com\/images\/themes\/theme1\/bg.png", 
     "profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/62250922\/Garrett_and_Michael_8_X_10_2_22-54-34_normal.jpg", 
     "utc_offset":-21600, 
     "profile_link_color":"0084B4", 
     "followers_count":12883, 
     "listed_count":542,"name":"Garrett Weber-Gale", 
     "notifications":false, 
     "protected":false,"id_str":"16819632", 
     "profile_use_background_image":true, 
     "screen_name":"G_WeberGale", 
     "is_translator":false, 
     "profile_text_color":"333333", 
     "profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/62250922\/Garrett_and_Michael_8_X_10_2_22-54-34_normal.jpg", 
     "friends_count":3758, 
     "description":"Co-founder AthleticFoodieTM. High blood pressure warrior. In love w food\/cooking! Working 2 get better @ everything", 
     "profile_background_image_url_https":"https:\/\/si0.twimg.com\/images\/themes\/theme1\/bg.png", 
     "default_profile_image":false, 
     "url":"http:\/\/www.athleticfoodie.com", 
     "profile_sidebar_border_color":"C0DEED", 
     "favourites_count":20, 
     "created_at":"Fri Oct 17 03:35:09 +0000 2008", 
     "contributors_enabled":false, 
     "geo_enabled":true, 
     "profile_background_tile":false, 
     "lang":"en","profile_sidebar_fill_color":"DDEEF6", 
     "show_all_inline_media":false, 
     "follow_request_sent":false, 
     "location":"Austin, Texas" 
    }, 
    "retweet_count":0, 
    "coordinates":null, 
    "in_reply_to_user_id":null, 
    "source":"\u003Ca href=\"http:\/\/www.hootsuite.com\" rel=\"nofollow\"\u003EHootSuite\u003C\/a\u003E", 
    "in_reply_to_status_id_str":null, 
    "id_str":"203565761040629760", 
    "place":null, 
    "in_reply_to_status_id":null, 
    "id":203565761040629760, 
    "possibly_sensitive":false, 
    "retweeted":false, 
    "text":"Awesome! Good for you:) \n\nRT @ATXglutenfree: My #startup, Locate Special Diet, has officially launched! http:\/\/t.co\/KyeDWZYj" 
}] 
}; 

var twitter_peeps = { 
twitterFeedData: 
    [{ 
    "created_at":"Fri May 18 19:20:34 +0000 2012", 
    "user":{ 
     "profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/62250922\/Garrett_and_Michael_8_X_10_2_22-54-34_normal.jpg", 
     "screen_name":"G_WeberGale", 
    }, 
    "id_str":"203565761040629760", 
    "text":"Awesome! Good for you:) \n\nRT @ATXglutenfree: My #startup, Locate Special Diet, has officially launched! http:\/\/t.co\/KyeDWZYj" 
    }] 
}; 

回答

0

您可以使用Json.NET將來自Web服務的響應解析爲JArray。然後,您可以從所需的響應中選擇唯一所需的數據。您還可以創建一個包含所需屬性的自定義對象,以將部分響應反序列化爲。

這裏有一個例子,你手動自定義對象加載到響應清單:

string json = "{\n\"twitterFeedData\":\n [{\n \"in_reply_to_user_id_str\":null,\n \"truncated\":false,\n \"contributors\":null,\n \"created_at\":\"Fri May 18 19:20:34 +0000 2012\",\n \"in_reply_to_screen_name\":null,\n \"favorited\":false,\n \"geo\":null,\n \"user\":{\n \"id\":16819632,\n \"verified\":false,\n \"default_profile\":true,\n \"profile_background_color\":\"C0DEED\",\n \"statuses_count\":8595,\n \"following\":true,\n \"time_zone\":\"Central Time (US & Canada)\",\n \"profile_background_image_url\":\"http:\\/\\/a0.twimg.com\\/images\\/themes\\/theme1\\/bg.png\",\n \"profile_image_url\":\"http:\\/\\/a0.twimg.com\\/profile_images\\/62250922\\/Garrett_and_Michael_8_X_10_2_22-54-34_normal.jpg\",\n \"utc_offset\":-21600,\n \"profile_link_color\":\"0084B4\",\n \"followers_count\":12883,\n \"listed_count\":542,\"name\":\"Garrett Weber-Gale\",\n \"notifications\":false,\n \"protected\":false,\"id_str\":\"16819632\",\n \"profile_use_background_image\":true,\n \"screen_name\":\"G_WeberGale\",\n \"is_translator\":false,\n \"profile_text_color\":\"333333\",\n \"profile_image_url_https\":\"https:\\/\\/si0.twimg.com\\/profile_images\\/62250922\\/Garrett_and_Michael_8_X_10_2_22-54-34_normal.jpg\",\n \"friends_count\":3758,\n \"description\":\"Co-founder AthleticFoodieTM. High blood pressure warrior. In love w food\\/cooking! Working 2 get better @ everything\",\n \"profile_background_image_url_https\":\"https:\\/\\/si0.twimg.com\\/images\\/themes\\/theme1\\/bg.png\",\n \"default_profile_image\":false,\n \"url\":\"http:\\/\\/www.athleticfoodie.com\",\n \"profile_sidebar_border_color\":\"C0DEED\",\n \"favourites_count\":20,\n \"created_at\":\"Fri Oct 17 03:35:09 +0000 2008\",\n \"contributors_enabled\":false,\n \"geo_enabled\":true,\n \"profile_background_tile\":false,\n \"lang\":\"en\",\"profile_sidebar_fill_color\":\"DDEEF6\",\n \"show_all_inline_media\":false,\n \"follow_request_sent\":false,\n \"location\":\"Austin, Texas\"\n },\n \"retweet_count\":0,\n \"coordinates\":null,\n \"in_reply_to_user_id\":null,\n \"source\":\"\\u003Ca href=\\\"http:\\/\\/www.hootsuite.com\\\" rel=\\\"nofollow\\\"\\u003EHootSuite\\u003C\\/a\\u003E\",\n \"in_reply_to_status_id_str\":null,\n \"id_str\":\"203565761040629760\",\n \"place\":null,\n \"in_reply_to_status_id\":null,\n \"id\":203565761040629760,\n \"possibly_sensitive\":false,\n \"retweeted\":false,\n \"text\":\"Awesome! Good for you:) \\n\\nRT @ATXglutenfree: My #startup, Locate Special Diet, has officially launched! http:\\/\\/t.co\\/KyeDWZYj\"\n }]\n}"; 

JObject obj = JObject.Parse(json); 
JArray arr = JArray.Parse(obj["twitterFeedData"].ToString()); 

List<TwitterResponse> responses = new List<TwitterResponse>(); 

foreach (var item in arr) 
{ 

    TwitterResponse tr = new TwitterResponse() 
    { 
     CreatedAt = item["created_at"].Value<string>(), 
     IdStr = item["id_str"].Value<string>(), 
     Text = item["text"].Value<string>(), 
     User = new User() { 
      ProfileImageUrl = item["user"]["profile_image_url"].Value<string>(), 
      ScreenName = item["user"]["screen_name"].Value<string>() 
     } 
    }; 

    responses.Add(tr); 
} 

class TwitterResponse 
{ 
    public string CreatedAt { get; set; } 
    public string Text { get; set; } 
    public string IdStr { get; set; } 
    public User User { get; set; } 
} 
class User 
{ 
    public string ProfileImageUrl { get; set; } 
    public string ScreenName { get; set; } 
}