Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
收缩/剥离给定的JSON对象_Json_Json.net - Fatal编程技术网

收缩/剥离给定的JSON对象

收缩/剥离给定的JSON对象,json,json.net,Json,Json.net,一个.net服务从twitter上给了我这个,我只需要大约五个字段,我已经完成了JSON的提供,只是JSON对象的数组可能非常大,似乎没有必要向客户端提供一个大的JSON文件,而不是一个小的,例如,JSON中只包含一个对象,可能非常多。。问题是如何在js文件中提供它之前进行收缩,以便以某种后端方式进行解析,从而只提供我需要的属性 var twitter_peeps = { twitterFeedData: [{ "in_reply_to_user_id_str":null, "

一个.net服务从twitter上给了我这个,我只需要大约五个字段,我已经完成了JSON的提供,只是JSON对象的数组可能非常大,似乎没有必要向客户端提供一个大的JSON文件,而不是一个小的,例如,JSON中只包含一个对象,可能非常多。。问题是如何在js文件中提供它之前进行收缩,以便以某种后端方式进行解析,从而只提供我需要的属性

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"
  }]
};
您可以使用将来自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; }
}
string json=“{\n\”twitterFeedData\”:\n[\n\”在回复用户id时\u str\”:null、\n\“被截断的\”:false、\n\“贡献者\”:null、\n\“创建于\\”:“2012年5月18日星期五19:20:34+0000\”,\n\“在回复屏幕名称时\u\”:null、\n\“偏好\”:false、\n\“geo\:null、\n\”用户id:\n\“验证过的\\”\n\”id:\n\“默认值:\n\”:false\ \ \:9632档案背景颜色:“\n档案背景图像url\”:“http:\/\\/a0.twimg.com\\/images\\\/theme1\\/bg.png\”,\n“档案背景图像url\”:“http:\\/\\\/a0.twimg.com\/images\\\/themes\\\/theme1\\\/bg.png\”,\n“档案图像url\”http:\/\\/a0.twimg.com\\/profile\u images\\/62250922\\/Garrett\u和Michael\u 8\u X\u 10\u 2\u 22-54-34\u normal.jpg\,\n“utc\u offset\”:-21600,\n“profile\u link\u color\”:“0084B4\,\n“followers\u count”:12883、\n“列出的\u count”:542、\n“姓名”:“Garrett-Weber-Gale\”,\n“虚假通知”;\n\n“虚假通知”;\n\n\n\n\n\u个人资料使用背景图片:真,屏幕名称:G\U WeberGale,\n是翻译:假,个人资料文本颜色:333333,个人资料图片url:https:“https:\/\/si0.twimg.com\/profile\\/images\/62250922\/Garrett\/u和Michael\\\\/u8\\ ux\\ u10\\ u2\\ u22-54-34,\ friends\\\ friends\\ n\\\\\\\\描述:3758联合创始人AthleticFooditem.高血压勇士.恋爱中的食物/烹饪!工作2变得更好@everything\,\n“个人资料背景图片url https\:”https:\/\\\/si0.twimg.com\\\/images\\\\/theme1\\/bg.png\,\n“默认个人资料图片”:false,\n“url\:”http:\/\\\\\\/www.athleticfoodie.com\,\n“个人资料边栏颜色:”C0dect\,\n\“Favorites\u count\”:20、\n\“created\u at\”:“Fri Oct 17 03:35:09+0000 2008\”,\n\“Contributs\u enabled\”:false、\n\“geo\u enabled\”:true、\n\“profile\u background\u tile\”:false、\n\“profile\u sidebar\u fill\u color\”:“DDEEF6\”,\n\“show\u所有在线媒体”:false、\n\“follow\u请求”:false、\n\u发送地点:德克萨斯州奥斯汀目前,网络环境中的网络用户还没有使用该用户的身份。目前,网络环境中的网络环境中的网络环境中的网络环境中的网络环境中的网络环境中的网络环境中的网络环境中的网络环境中的网络环境中的网络环境中的网络环境中的网络环境中的网络环境中的网络环境中的网络环境中的网络环境中的网络环境中的网络环境中的网络环境中的网络环境中的网络环境中的网络用户用户的用户的用户的用户的用户的用户的用户的身份id的身份id的身份身份身份id id:目前,网络环境中的网络环境中的网络环境中,网络环境中的网络环境中,网络环境中的网络环境中的网络环境中的网络环境中的网络源\\n\n\\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\来源来源:::::以下以下以下以下以下以下以下以下以下以下以下以下以下以下以下以下以下“:null,\n\”id\“:203565761040629760、\n \“可能敏感\”false、\n \“转发\”false、\n \“文本\”false、\n \“太棒了!对你有好处:)\\n\\nRT@atx麸质免费:我的“启动,定位特殊饮食”,已经正式启动!http:\/\\\/t.co\/KyeDWZYj\”\n}\n}”;
JObject obj=JObject.Parse(json);
JArray arr=JArray.Parse(obj[“twitterFeedData”].ToString());
列表响应=新列表();
foreach(arr中的var项目)
{
TwitterResponse tr=新的TwitterResponse()
{
CreatedAt=item[“created_at”].Value(),
IdStr=item[“id_str”].Value(),
Text=项[“Text”]。值(),
User=新用户(){
ProfileImageUrl=item[“user”][“profile\u image\u url”]。Value(),
ScreenName=项目[“用户”][“屏幕名称”]。值()
}
};
答复.添加(tr);
}
类twitter响应
{
公共字符串CreatedAt{get;set;}
公共字符串文本{get;set;}
公共字符串IdStr{get;set;}
公共用户{get;set;}
}
类用户
{
公共字符串ProfileImageUrl{get;set;}
公共字符串屏幕名称{get;set;}
}