简体   繁体   English

复杂的JSON结构

[英]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. 我要处理前端开发(AngularJS),而不是从后端提取数据(该数据尚不完整,但会将所有内容呈现为JSON),而是使用硬编码JSON。

However, I am new to this and can't seem to find anything about complex JSON structure. 但是,我对此并不陌生,似乎找不到任何有关复杂JSON结构的信息。 In a basic sense, my web app has users and the content they create. 从根本上讲,我的Web应用程序包含用户及其创建的内容。 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?). 也许我什至完全不正确地处理此问题(例如,CRUD与REST?这有关系吗?据我所知,REST意味着上面的每个对象都是具有自己唯一URI的资源?因此,渲染的JSON是否会受到影响?)。 I really am not sure. 我真的不确定。 But ultimately, I need to use the JSON structure properly pull data into my frontend. 但最终,我需要使用JSON结构将数据正确提取到前端。 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. 我猜比JSON本身还多,我的意思是数据的结构。 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. 也许我的问题甚至与JSON无关,而不仅仅是以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: 至于Angular的东西,如果您想直接从JSON文件中提取作为测试,请看这里:

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. 我还建议考虑部署部署,以进行开发而无需访问实时数据服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 复杂的JSON结构+ Dojo选择 - Complex JSON structure + Dojo Selects React JS-解析复杂的Json结构并在下拉列表中填充 - React JS - Parse a complex Json structure and Populate in a dropdown list jQuery通过“ where子句”遍历复杂的JSON结构 - jquery each iterating over complex JSON structure with “where clause” 如何在angular2 [typescript]中定义具有复杂json结构的模型? - How to define model with complex json structure in angular2 [typescript]? 需要将复杂的json obj转换为合适的angularjs ui树数据json结构 - need to convert complex json obj to suitable angularjs ui tree data json structure 创建复杂的表结构 - Create complex table structure 想要更改复杂的 Json 结构,我需要在 Javascript 中更改它的数组 - Want to change complex Json structure which I need change it array in Javascript 根据使用 Javascript Map 或过滤器方法从复杂的 JSON 结构中选择 id 来显示国家/地区 - display countries based on selection of id from a complex JSON structure using Javascript Map or Filter Method 从复杂的 json 结构中获取最后一个子节点,并仅重构 javascript 中的最后一个节点 - Get the last child node from complex json like structure and restructure only the last node in javascript 如何访问复杂结构的数据? - How to access data in complex structure?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM