JSON无法获取数据(JSON cannot get data)
嗨,我正在尝试解析C#中的JSON页面,但我似乎无法读取一些字符串,例如:
"versions": [ { "date": 1340466559, "dl_link": "http://dev.bukkit.org/media/files/599/534/NoSwear.jar", "filename": "NoSwear.jar", "game_builds": [ "CB 1.2.5-R4.0" ], "hard_dependencies": [], "md5": "d0ce03e817ede87a9f76f7dfa67a64cb", "name": "NoSwear v5.1", "soft_dependencies": [], "status": "Semi-normal", "type": "Release" },
我怎么能读到这个? 这是我现在拥有的代码,除了这个以外的所有其他代码:
public static void GetMoreInfo(string plugin) { try { string url = "http://bukget.org/api/plugin/"; var wc = new WebClient(); var json = wc.DownloadString(url + plugin); var moreInfo = JsonConvert.DeserializeObject<MoreInfo>(json); foreach (var category in moreInfo.categories) { Categories += category + ", "; } Categories = Categories.Remove(Categories.Length - 2, 2); } catch (Exception) { } finally { if (string.IsNullOrEmpty(Categories)) { Categories = "No data found."; } } } public class MoreInfo { public string[] categories; }
Hi I'm trying to parse a JSON page in C#, but I can't seem to read some strings, like:
"versions": [ { "date": 1340466559, "dl_link": "http://dev.bukkit.org/media/files/599/534/NoSwear.jar", "filename": "NoSwear.jar", "game_builds": [ "CB 1.2.5-R4.0" ], "hard_dependencies": [], "md5": "d0ce03e817ede87a9f76f7dfa67a64cb", "name": "NoSwear v5.1", "soft_dependencies": [], "status": "Semi-normal", "type": "Release" },
How could I read that? This is the code I have right now and works for everything else except this:
public static void GetMoreInfo(string plugin) { try { string url = "http://bukget.org/api/plugin/"; var wc = new WebClient(); var json = wc.DownloadString(url + plugin); var moreInfo = JsonConvert.DeserializeObject<MoreInfo>(json); foreach (var category in moreInfo.categories) { Categories += category + ", "; } Categories = Categories.Remove(Categories.Length - 2, 2); } catch (Exception) { } finally { if (string.IsNullOrEmpty(Categories)) { Categories = "No data found."; } } } public class MoreInfo { public string[] categories; }
原文:
最满意答案
这里的解决方案是获取您需要的所有数据并创建一个类别数组,每个类别都包含自己的子类别。 并在嵌套的
ng-repeats
使用此对象。请检查我的更新版本的plunker:
https://plnkr.co/edit/BaHOcI4Qa8t5CkbshyCX
如你所见,我使用:
ng-repeat="category in documents.categories"
在外部
ng-repeat
和:ng-repeat="service in category.subCategories"
在内在的。
在Controller中,我按以下方式构建
category.subCategories
:documentsFactory .getCategories() // Get categories .then(function(response) { // Extract them from the response return response.data.subCategory; }) .then(getSubcategories) // For every catgory, get its own subcategory .then(function(categories) { // Add categories to the $scope self.categories = categories; });
getSubcategories
是:function getSubcategories(categories) { console.log(categories) var retrievSubcategories = categories.map(function(category) { // Create a promise to get the subcategories for every category return documentsFactory .getServicesByCategory(category.name) // Get the subcategories for the given category .then(function(response) { category.subCategories = response.data.documents; // Add subgategories to the category return category; }); }); return $q.all(retrievSubcategories) // Return a promise resolve only when all the subcategories for every category are retrieved }
The solution here is to get all the data you need and create an array of categories, each one containing its own sub categories. And use this object in the nested
ng-repeats
.Please check my updated version of your plunker:
https://plnkr.co/edit/BaHOcI4Qa8t5CkbshyCX
As you can see, I use:
ng-repeat="category in documents.categories"
In the outer
ng-repeat
and:ng-repeat="service in category.subCategories"
In the inner one.
In the Controller I build the
category.subCategories
in the following way:documentsFactory .getCategories() // Get categories .then(function(response) { // Extract them from the response return response.data.subCategory; }) .then(getSubcategories) // For every catgory, get its own subcategory .then(function(categories) { // Add categories to the $scope self.categories = categories; });
Where
getSubcategories
is:function getSubcategories(categories) { console.log(categories) var retrievSubcategories = categories.map(function(category) { // Create a promise to get the subcategories for every category return documentsFactory .getServicesByCategory(category.name) // Get the subcategories for the given category .then(function(response) { category.subCategories = response.data.documents; // Add subgategories to the category return category; }); }); return $q.all(retrievSubcategories) // Return a promise resolve only when all the subcategories for every category are retrieved }
相关问答
更多-
如何循环使用ng-repeat功能返回的项目?(How to Loop through items returned by a function with ng-repeat?)[2021-10-10]
简短的答案 :你真的需要这样的功能,还是可以使用财产? http://jsfiddle.net/awnqm/1/ 长答案 为了简单起见,我将仅描述您的案例 - ngRepet对象的数组。 另外,我省略一些细节。 AngularJS使用脏检查来检测变化。 当应用程序启动时,它会为$rootScope运行$digest 。 $digest将对范围的层次结构进行深度优先遍历。 所有范围都有手表列表。 每个手表都有最后的值(最初是initWatchVal )。 对于所有手表的每个范围, $digest运行它,获取当 ... -
您不能在视图中使用每次都返回不同值的函数。 每个摘要周期,Angular执行多个摘要,直到范围值稳定(与上一个摘要相同)。 如果该范围中的值永远不会稳定,则最终会得到无限的摘要,并且在角度中止之前默认限制为10。 我建议首先在控制器中构建一个随机颜色数组,然后在ng-repeat使用$index来获取每个值: this.randomColors = this.profile.interests.map(function(){ return ('#' + Math.floor(Math.random( ...
-
问题在于这一行(在@Raulucco解释的摘要周期中): ng-style="{background: styleBuilder(person.options)}" 尝试ng-init:
-
搜索了很多之后,我认为我的代码中没有错误。 正如这里所回答的和在这个plunkr中所显示的那样,角度是如何工作的。 似乎没有其他解决方案,除了设置一个更高的摘要TTL数字比10。 angular.module('myApp',[]) .config(function($rootScopeProvider) { $rootScopeProvider.digestTtl(number); //some number bigger then 10 }) After searching a lot, I think ...
-
你不能在div上使用ng-change ,它只适用于复选框,收音机,文本等输入元素。 这是要求ng-model ..即使用ng-change你将不得不强制使用ng-model 。 在你的情况下使用ng-click 。 这是ng-change的文档 。 You cant use ng-change on a div it is only applicable for input elements like checkbox, radio, text etc. It is asking for ng-model ...
-
$timeout(resetHeight, 1000)导致Infinite $digest loop 。 这是因为resetHeight方法正在更改变量$scope.docHeight并启动另一个$timeout循环, $timeout触发$scope.$apply() 。 内部Angular手表可能看起来是,或实际上是,并且无休止的$digest loop ,如果它发现这个条件抛出异常。 我必须做一些类似于你工作的东西,我将不会在Window Resize事件上做Debouncing逻辑。 这是因为在调整 ...
-
问题在于你正在调用一个函数,这将导致修改$scope的绑定值,这又会再次执行该函数......等等...... 你应该改变这一点代码: {{ getTipCount(worker.id) }} 像这样的东西: {{ workertipscount }} 此外,这似乎是您在此处设置的全局变量: $scope.workertipscount = res.worker.tips.lengh 相反,您可以直接将其设置到工作对象本身上: