简体   繁体   中英

Complex JSON Structure

I am tackling frontend development (AngularJS) and rather than pull data from the backend (which isn't complete but renders everything to JSON), I am looking to just use hardcoded JSON.

However, I am new to this and can't seem to find anything about complex JSON structure. In a basic sense, my web app has users and the content they create. So, in my mind, there will be two databases, but I'm not sure if I'm approaching it correctly.

  1. Users - username, location, created content, comments, etc.

     "user": [ { "userID": "12", "userUserName": "My Username", "userRealName": "My Real Name", "mainInterests": [ { "interest": "Guitar" }, { "interest": "Web Design" }, { "interest": "Hiking" } ], "userAbout": "All about me...", "userComments": [ { "comment": "this is a comment", "contentID" : "12" }, { "comment": "this is another comment", "contentID" : "123" } ], } ] 
  2. Created Content - title, description, username, comments, etc.

     "mainItem": [ { "mainID": "1", "mainTitle": "Guitar Lessons", "mainCreatorUserName": "My Username", "mainCreatorRealName": "My Real Name", "mainTags": [ { "tag": "Intermediate" }, { "tag": "Acoustic" }, { "tag": "Guitar" } ], "mainAbout": "Learn guitar!", "mainSeries": [ { "videoFile": "file.mov", "thumbnail": "url.jpg", "time": "9:37", "seriesNumber": "1", "title": "Learn Scales" }, { "videoFile": "file.mov", "thumbnail": "url.jpg", "time": "8:12", "seriesNumber": "2", "title": "Learn Chords" } ], "userComments": [ { "comment": "this is a comment", "userID" : "12" }, { "comment": "this is another comment", "userID" : "123" } ] } ] 

And there is more complexity than that, but I just would like to understand if I'm approaching this right. Maybe I'm even approaching this entirely incorrectly (for instance, CRUD vs. REST? Does it matter here? As I understand it, REST implies that each of the objects above are resources with their own unique URI? So would JSON rendered be impacted?). I really am not sure. But ultimately, I need to use the JSON structure properly pull data into my frontend. Assumably, whatever said structure is will be mirrored and rendered in the backend.

Edit* thank you guys for the replies. I think part of my question, where I clarify "complex", is missing. So I'd like to explain. I guess more than the JSON itself, I really mean the structure of the data. For instance, in my example, I am structuring my data to all be beneath two unique objects (user and content). Is this correct? Or should I think about my data more diverse? For instance, technically I could have a comments database (where each comment is the main object). Or is that still implied in my dataset? Perhaps my question isn't even about JSON as much as it is the data structure which will happen to get rendered in JSON. Hopefully this clarifies what I mean by complex?

Any and all help is appreciated.

I'm not sure why you're making what seems to be objects into single-item arrays (as evidenced by the opening square brackets). Other than that, it looks fine to me. Generally speaking single items (like "User") are structured as an object and multiples are arrays of objects.

As for the Angular stuff, if you want to pull direct from a JSON file as a test, take a look here:

var services = angular.module('app.services', [])

services.factory('User', function($http) {
    var User = function(data) { 
        return data;
    }

    User.findOne = function(id) {
        return $http.get('/test_user.json').then(function(response) { 
            return new User(response.data); 
        });
    };

    return User;
});

I also recomment looking into Deployed for doing development without access to live data services.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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