首页 \ 问答 \ JSON无法获取数据(JSON cannot get data)

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;
}

原文:
更新时间:2022-07-20 10:07

最满意答案

这里的解决方案是获取您需要的所有数据并创建一个类别数组,每个类别都包含自己的子类别。 并在嵌套的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
}

相关问答

更多
  • 搜索了很多之后,我认为我的代码中没有错误。 正如这里所回答的和在这个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 相反,您可以直接将其设置到工作对象本身上: