dcsimg
www.webdeveloper.com
Results 1 to 2 of 2

Thread: I need help getting data out of a json file

  1. #1
    Join Date
    May 2017
    Posts
    1

    I need help getting data out of a json file

    Hi all,

    I am completely new at JavaScript and I need to create a Web app, which provides functionality to choose a person within the group stored in the database (json file) and display those people who are directly connected to the chosen user.

    The file looks something like this:

    [
    {
    "id": 1,
    "firstName": "Paul",
    "surname": "Crowe",
    "age": 28,
    "gender": "male",
    "friends": [
    2
    ]
    },
    {
    "id": 2,
    "firstName": "Rob",
    "surname": "Fitz",
    "age": 23,
    "gender": "male",
    "friends": [
    1,
    3
    ]


    Any ideas how I might approach this problem?

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,392
    You parse the string with JSON.parse and then you have a variable.

    Using the example you provided...
    Code:
    [ { "id": 1,
    "firstName": "Paul",
    "surname": "Crowe",
    "age": 28,
    "gender": "male",
    "friends": [2]
    },
    { "id": 2,
    "firstName": "Rob",
    "surname": "Fitz",
    "age": 23,
    "gender": "male",
    "friends": [1,3]
    }
    ]
    and then using

    Code:
     var x = JSON.parse( "theJSONString" );
    then x[0].id = 1, x[0].firstname = Paul and x[1].age = 23

    "theJSONString" is the variable you dumped the retrieved string from, I assume it was an AJAX call that resulted in the JSON file to be returned by it, if you obtained the string by any other means, then please let us know as it will require some different handling.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center