Jump to content

Preluare date din fisier JSON in Javascript


Recommended Posts

Salut, cum as putea prelua datele dintr-un fisier JSON cu ajutorul unor parametri folosind javascript?

Date din fisierul json

{
	"Nume1": {
        	"nume": "Nume complet",
		"descriere": "Descriere persoana"
 	   },
	
	"Nume2": {
      		  "nume": "Nume complet2",
		"descriere": "Descriere persoana2"
   	 },
	
	"Nume3": {
       		 "nume": "Nume complet3",
		"descriere": "Descriere persoana 3"
	}
}

Pentru "Nume1" cum as putea prelua "nume" si "descriere" cu ajutorul parametrilor?
 

Am incercat sa preiau datele din acelasi fisier, dar primesc eroarea "TypeError: membri.x is undefined"

Cod folosit

var membri = [{
	"Nume1": {
        	"nume": "Nume complet",
		"descriere": "Descriere persoana"
 	   },
	
	"Nume2": {
      		  "nume": "Nume complet2",
		"descriere": "Descriere persoana2"
   	 },
	
	"Nume3": {
       		 "nume": "Nume complet3",
		"descriere": "Descriere persoana 3"
	}
}];

function load(x,y) {
  alert(membri[0].x.y);
}

 

Link to post
Share on other sites

Rezolvat

function test() {
  $.ajax({
   url: 'detalii.json',
   dataType: 'json',
  success: function(resp) {
    var jsn =  resp;
	<!-- alert(jsn); -->
    let nume1 = jsn.Nume1.nume;
    let desc1 = jsn.Nume1.descriere;

     alert(nume1 +' - '+ desc1);
   },
    statusCode: {
       404: function() {
         alert('There was a problem with the server.  Try again soon!');
       }
     }
  });
};

 

  • Like 1
  • Downvote 1
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.