Erçin Dedeoğlu
Erçin Dedeoğlu

Reputation: 5383

How can I get specific data from JSON?

How can I get specific data from JSON?

JSON:

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

Javascript Code:

<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. 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:

Current 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.

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

With my code and given examples I still couldn't get the TotalPageCount and TotalTitleCount.

For the record: Maybe next time I can add more attributes next to Title. So I would like to keep that json format.

Thanks for advance

Upvotes: 0

Views: 182

Answers (4)

TiiJ7
TiiJ7

Reputation: 3412

With your current JSON format, it'd be something like this. It'd be better if you could change the JSON format, though.

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

Upvotes: 0

TonyMtz
TonyMtz

Reputation: 46

Are you using jQuery right? 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. 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.

Upvotes: 0

Emre Erkan
Emre Erkan

Reputation: 8482

When you do x in data you get every key of objects. So you have to do a simple check if it's TotalPageCount or 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>

Upvotes: 1

adeneo
adeneo

Reputation: 318342

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

Upvotes: 0

Related Questions