如何从Angular 2中的JSON数据中获取数组

时间:2017-01-29 13:25:49

标签: json angular

我正在开发一个角度为2的应用程序。我已经创建了一个服务,现在我想从json数据中获取特定的数组,我将如何获得。

我有这样的服务代码

getProjects(): Promise<Project>{
return this.getData()
  .toPromise()
  .then(response => response.json())
  .catch((error: string) => console.log(error));
}

这会返回所有json数据,但我只想从json中获取“Projects”数组。

JSON代码

{
   "features":[],
   "posts":{},
   "Projects":[
    {
        "id":"proj-1",
        "name":"Design for creative",
        "meta":{
            "date":"03 November, 2016",
            "url":"http://www.pixelsocket.com",
            "skills":[
                "ui design",
                "programming",
                "marketing"
            ]
        },
        "images":[
            "assets/images/design-creative-01.jpg",
            "assets/images/design-creative-02.jpg",
            "assets/images/design-creative-03.jpg",
            "assets/images/design-creative-04.jpg"
        ],
        ...
}

更新

getProject(id: string): Promise<Project>{
 const url = `${this.getProjects()}/${id}`; // can i call getProjects() method here

 return this.http.get(url)
   .toPromise()
   .then(response => response.json())
   .catch((error: string) => console.log(error));
}

2 个答案:

答案 0 :(得分:1)

使用方法转换json,

<html>
<body bgcolor="cyan">
<h1>The Solar System</h1>
<script>
var planets= ["Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune"];
var endofplanets=planets.length;

var i = -1;

function nextplanet()
{
  i=i+1;

  if(i==endofplanets){
    i=0;
  }

document.getElementById('p1').innerHTML=planets[i];

}

function previousplanet(){

  i=i-1;
  if(i<0){
    i=endofplanets-1;}

  document.getElementById('p1').innerHTML=planets[i];


}
</script>
<p id="p1">---</p>
<button type="button" onclick="nextplanet()">Next Planet</button>
<button type="button" onclick="previousplanet()">Previous Planet</button>
</body>
</html>

答案 1 :(得分:0)

 private class CallStateListener extends PhoneStateListener {
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                switch (state) {
                case TelephonyManager.CALL_STATE_RINGING:
                    number = incomingNumber;
                    Thread_calls.run();
                    //Toast.makeText(ctx, "Incoming: " + incomingNumber,Toast.LENGTH_LONG).show();
                    break;
                }
            }
        }

        public class OutgoingReceiver extends BroadcastReceiver {
            public OutgoingReceiver() {
            }

            @Override
            public void onReceive(Context context, Intent intent) {
                number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
                Thread_calls.run();
                //Toast.makeText(ctx, "Outgoing: " + number, Toast.LENGTH_LONG).show();
            }

        }
getProjects(): Promise<Project>{
return this.getData()
  .toPromise()
  .then(response => response.json().projects) // <<< added .projects
  .catch((error: string) => console.log(error));
}