John S.
John S.

Reputation: 29

Javascript (Ajax) Parse JSON value

total javascript noob here. Just trying to get an understanding for the language.

I'm requesting a JSON request using the following code:

function request(){
$.ajax({
        dataType: "jsonp",
        type: 'GET',
url: "getWebsite",
        success: function(result){
            data = result;
            $('.data').text(data);
            console.log(data);
    }});

The get request returns something like this:

"items": [ 
 {
  "topLevelComment": {
    "authorDisplayName": "a"
    "textDisplay": "b"
 },
 {
  "topLevelComment": {
    "authorDisplayName": "c"
    "textDisplay": "d"
 }

I would like to cycle through the AuthorDisplayName and textDisplay and randomly pick one from each. The best way to do this would probably be to put them both into arrays if I had to guess. I'm not sure how to even go about this.

Upvotes: 0

Views: 92

Answers (4)

Farhad Bagherlo
Farhad Bagherlo

Reputation: 6699

var json={
	"items": [{
		"topLevelComment": {
			"authorDisplayName": "a",
			"textDisplay": "b"
		}
	}, {
		"topLevelComment": {
			"authorDisplayName": "c",
			"textDisplay": "d"
		}
	}, {
		"topLevelComment": {
			"authorDisplayName": "e",
			"textDisplay": "f"
		}
	}, {
		"topLevelComment": {
			"authorDisplayName": "g",
			"textDisplay": "h"
		}
	}]
};
$("input:button").on("click",function(){
 selectRand = Math.floor((Math.random() * json.items.length))
 var r=json.items[selectRand].topLevelComment.textDisplay;
 console.log(r);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" value="selectRand"/>

Upvotes: 1

Viki Theolorado
Viki Theolorado

Reputation: 556

If your data is already in Object format, and you only want to get one from random pick. Then you don't have to loop for all the data. Just randomly select one index from your total data.

function request(){
    $.ajax({
        dataType: "jsonp",
        type: 'GET',
        url: "getWebsite",
        success: function(result){
            data = result;
            $('.data').text(data);
            console.log(data);

            var randomIndex = Math.floor((Math.random() * data.items.length));
            console.log("Selected data authorDisplayName: " + data.items[randomIndex].topLevelComment.authorDisplayName);
            console.log("Selected data textDisplay: " + data.items[randomIndex].topLevelComment.textDisplay);
        }
    });
}

Upvotes: 0

Boris Yordanov
Boris Yordanov

Reputation: 1

As far as i understood you are trying to display a random object from the items array?

The items variable is already an array, so you don't need to create one. To get a random element of the array you can use the following code:

var item = result.items[Math.floor(Math.random()*items.length)];

I'm not sure where exactly the items array is located, let's say it's on the root of the result. You will probably also need to run the array through the method JSON.parse() to make it a valid JavaScript object.

And then to get the text and display name you can do this:

var authour = item.topLevelComment.authorDisplayName; var text = item.topLevelComment.textDisplay;


Upvotes: 0

Sebastian
Sebastian

Reputation: 1722

items is already an array. So you do the following:

  1. Parse the result to json (Only if a string is returned)

    items = JSON.parse(items)

  2. Get a random index

    let index = Math.floor((Math.random() * items.length))

  3. Get the random data

    let authorDisplayName = items[index].topLevelComment.authorDisplayName
    let textDisplay = items[index].topLevelComment.textDisplay

Upvotes: 0

Related Questions