简体   繁体   English

如何从JSON获取特定数据?

[英]How can I get specific data from JSON?

How can I get specific data from JSON ? 如何从JSON获取特定数据?

JSON: JSON:

[
    { "TotalPageCount":  66 },
    { "TotalTitleCount": 199 },
    { "Title":           "cola" },
    { "Title":           "elvis tom" },
    { "Title":           "dvd" }
]

Javascript Code: JavaScript代码:

<script>
    function updateTitlesArea() {
        $.getJSON(
        "/lates/json/",
        null,
        function(data) {
            $("#titlesDiv").html("");
            $("#pagesDiv").html("");
            var x;
            if (data.length > 0) {
                for (x in data) {
                    $("#titlesDiv").html($("#titlesDiv").html() +
                        "<li><a href=\"/" + data[x].Title.replace(/ /g, '-') + "\">" + data[x].Title + "</a>" +
                        "</li>"
                );
                }
            } else {
                $("#titlesDiv").html("<li>no entry</li>");
            }
        });
    }
</script>

I'm trying to get {"TotalPageCount":66} AND {"TotalTitleCount":199} from Javascript. 我正在尝试从Javascript获取{"TotalPageCount":66}{"TotalTitleCount":199} Please provide me a method to get them? 请提供一种获取方法的方法吗?

Thanks a lot. 非常感谢。

Update 更新

Something going wrong, I tried all solutions but not worked fine. 出问题了,我尝试了所有解决方案,但效果不佳。 I have control on json format: 我可以控制json格式:

Current Json Builder: 当前的Json Builder:

  if (title.Any()) { foreach (var x in title) { result.AddLast(new { Title = x.Title }); } } result.AddLast(new { TotalPageCount = totalPageCount }); result.AddLast(new { TotalTitleCount = totalTitleCount }); return Json(result.ToList(), JsonRequestBehavior.AllowGet); 

I made a small change in json format, took totalpage and title count to end of json. 我对json格式进行了一些小的更改,将总页数和标题计数增加到json的末尾。

Current: 当前:

[{"Title":"kola"},{"Title":"yilmaz ozdil"},{"Title":"dvd"},{"Title":"truly madly deeply"},{"Title":"deportivo de la coruna"},{"Title":"sizi seven kisiyle sevdiginiz kisinin farkli olmasi"},{"Title":"kadinlarin bavul gibi canta tasimalari"},{"Title":"hosbuldum"},{"Title":"sark cibani"},{"Title":"mevsimler gecerken"},{"Title":"bir kerede kolon gibi sicmak"},{"Title":"gelismek"},{"Title":"faz ve alasim bilimi"},{"Title":"memetq"},{"Title":"ogrencilerin sinav kagidina dustugu ilginc notlar"},{"Title":"cami duvarina isemek"},{"Title":"kufurden sonra tovbe etmek"},{"Title":"gida tarim ve hayvancilik bakanligi"},{"Title":"cevre orman ve sehircilik bakanligi"},{"Title":"google da nikah masasi calmak"},{"TotalPageCount":9},{"TotalTitleCount":199}] [{“ Title”:“ kola”},{“ Title”:“ yilmaz ozdil”},{“ Title”:“ dvd”},{“ Title”:“真正地深深地”},{“ Title”: de corvo de la coruna“},{” Title“:” sizi 7 kisiyle sevdiginiz kisinin farkli olmasi“},{” Title“:” kadinlarin bavul gibi canta tasimalari“},{” Title“:” hosbuldum“},{” Title “:” sark cibani“},{” Title“:” mevsimler gecerken“},{” Title“:” bir kerede kolon gibi sicmak“},{” Title“:” gelismek“},{” Title“:” faz ve alasim bilimi“},{” Title“:” memetq“},{” Title“:” ogrencilerin sinav kagidinaustugu ilginc notlar“},{” Title“:” cami duvarina isemek“},{” Title“:” kufurden sonra tovbe etmek“},{” Title“:” gida tarim ve hayvancilik bakanligi“”,{“ Title”:“ cevre orman ve sehircilik bakanligi”},{“ Title”:“ google da nikah masasi Calakak”},{“ TotalPageCount “:9},{” TotalTitleCount“:199}]

With my code and given examples I still couldn't get the TotalPageCount and TotalTitleCount. 通过我的代码和给出的示例,我仍然无法获得TotalPageCount和TotalTitleCount。

For the record: Maybe next time I can add more attributes next to Title . 作为记录:也许下次我可以在Title旁边添加更多属性。 So I would like to keep that json format. 所以我想保留json格式。

Thanks for advance 谢谢前进

When you do x in data you get every key of objects. 当您x in data您将获得对象的每个键。 So you have to do a simple check if it's TotalPageCount or TotalTitleCount . 因此,您必须简单检查它是TotalPageCount还是TotalTitleCount Like this; 像这样;

<script>
    function updateTitlesArea() {
        $.getJSON(
            "/lates/json/",
            null,
            function(data) {
                var x, totalPageCount, totalTitleCount;
                $("#titlesDiv").html("");
                $("#pagesDiv").html("");
                if (data.length > 0) {
                    for (x in data) {
                        if('TotalPageCount' == x) {
                            totalPageCount = data[x];
                            continue;
                        }
                        if('TotalTitleCount' == x) {
                            totalTitleCount = data[x];
                            continue;
                        }
                        $("#titlesDiv").html($("#titlesDiv").html() +
                            "<li><a href=\"/" + data[x].Title.replace(/ /g, '-') + "\">" + data[x].Title + "</a>" +
                            "</li>"
                        );
                    }
                    // You can do here whatever you want with 'totalPageCount' and 'totalTitleCount'
                } else {
                    $("#titlesDiv").html("<li>no entry</li>");
                }
            });
    }
</script>

It's an array, so 这是一个数组,所以

if (data.length > 0) {
    var obj = data[0];   // array, not object
     for (key in obj) { 
          $("#titlesDiv").html(
              $("#titlesDiv").html() +
                 "<li><a href=\"/" + obj[key].Title.replace(/ /g, '-') + "\">" + 
                     obj[key].Title + "</a>" +
                 "</li>"
          );
     }

Are you using jQuery right? 您使用的是jQuery吗? You can try something like this: 您可以尝试如下操作:

var prom = $.getJSON('/lates/json/'),
    ar;
prom.then(function (data) { ar = data; });

After that, you will have the response array in ar . 之后,您将在ar中拥有响应数组。 You will be able to access to your required values in this fashion: 您将可以通过以下方式访问所需的值:

ar[0].TotalPageCount
ar[1].TotalTitleCount

I hope this will help you. 我希望这能帮到您。 Good luck. 祝好运。

With your current JSON format, it'd be something like this. 使用您当前的JSON格式,将是这样。 It'd be better if you could change the JSON format, though. 不过,最好是可以更改JSON格式。

<script>
    function updateTitlesArea() {
        $.getJSON(
            "/lates/json/",
            null,
            function(data) {
                $("#titlesDiv").html('<ul></ul>'); // <li> goes inside <ul> or <ol>
                $("#pagesDiv").empty();

                var has_titles = false;

                for(var i = 0; i < data.length; i++) {
                    if("Title" in data[i]) {
                        $("#titlesDiv ul").append(
                            '<li><a href="/' + data[i].Title.replace(/ /g, '-') + '">' + data[i].Title + "</a>" +
                            "</li>"
                        );
                        has_titles = true;
                    }
                }
                if( !has_titles ) {
                    $("#titlesDiv ul").html("<li>no entry</li>");
                }
            }
        );
    }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM