如何计算json数据项ionic2 [英] How to count json data item ionic2

查看:100
本文介绍了如何计算json数据项ionic2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现一个新问题,
我想计数 @NAME 来设置数组 FACET [] for show @KEY



Myjson

RESULT:{
FACET:[
{@NAME:creator,
@COUNT:20,
FACET_VALUES:[
{
@KEY:Book Company1,
@VALUE:13},
{
@KEY:Book Company1,
@VALUE:10}],
{@NAME:lang,
@COUNT: 70,
FACET_VALUES:[
{
@KEY:tha,
@VALUE:33},
{
@KEY:eng,
@VALUE:42}
],
{@NAME :bnb,
@COUNT:64,
FACET_VALUES:[
{




]



  optionsFn():void {
this.http.get(my_url)
.subscribe(data => {
this .date = data.json()。RESULT.FACET; // get @NAME
},error => {
err => console.error(err),
() => console.log('getRepos completed')
);
console.log(this.date);

console.log(this.date);
this.goToapply()
}

goToapply(){

this.http.get(my_url)
.subscribe (data => {
this.foundRepos = data.json()。RESULT.FACET [0,1,2,3.4 ......]。FACET_VALUES; /// get @KEY
},error => {
console.log(error);
});


code

$ b $ p

 < ion-label> Filterr< / ion-label> 
< ion-option value =...* ngFor =let item of date> {{item [@ NAME]}},({{item [@ COUNT]} })< /离子选项>
< / ion-select>
< ion-list>
< ion-item * ngFor =let item of foundRepos(click)=itemClicked($ event,item)>
< h3> {{item [@KEY]}}< / h3>
< / ion-item>
< / ion-list>
pre>

这个 value =...我想保留它到 string

= 3(creator,lang,bnb)
,并获得 value =...= 0,1,2(0 == creator,1 == lang,2 == bnb)
当我选择 lang 时,我得到 1 ,I将它用于 FACET [1] .FACET_VALUES ,这样我得到 @KEY


抱歉写错了。我的英文不太好。

好的,我想指出一个小窍门,你应该用单词更好一些,尤其是英语,而不是英语,但更多的是解释你当前的结果和你想要的结果。迫使你以比现在快得多的速度学习英语(来自非母语者)



我希望计数@ NAME为show @KEY设置数组FACET []



因此,通过返回的JSON,我们可以做一些事情,从立即查看您的问题中就不会很清楚(因为我不相信这样做很糟糕质量或不是一个好问题)。



所以在我的帖子中应该包括3件事情




  • 计算NAME发生的金额

  • 为FACET设置数组
  • 显示每个FACET的KEY是最终目标。



确定

该名称发生



所以你基本上已经得到了这个答案。您提交了此代码

this.http.get(my_url)
.subscribe(data => {
this.date = data.json()。RESULT.FACET; // get @NAME
});



其中意味着您已经可以访问FACET阵列。通过创建一个for循环并检查名称是否未定义(因此递增一个int)或者计数来完成对NAME发生的数量进行计数,因为NAME始终被定义,因此只需调用 this.date.size()(如果this.date设置正确的话)



get FACET



的数组,您已经通过分析 this.date = data.json()来完成这项工作。 RESULT.FACET 。您的 this.date 现在包含一个包含FACET对象的数组。只是看到它是这样的:JSON(带有JSON中不可能的注释,但仅用于演示目的)=

  {FACET: //声明主对象,称为FACET 
[//将FACET声明为Array([]用于创建列表)
{id:2} //初始化FACET数组的子元素
,{id:3} //初始化第二个FACET子元素。






$ b因此,这个伪(不现实的)JSON包含2个对象,有 id = 2 id = 3



好吧,现在您应该了解一下JSON,让我们来看看您的代码的外观(将每个办公室的多个办公室和多名员工考虑在内的Businnes)

 {
OFFICES​​:[
{
id:1,
location:New York,
employees:[
{id:1,fullName:Jon Skeet},
{id:2,fullName:Random陌生人}
]
},
{
id:2,
location:Amsterdam,
employees :[
{id:1,fullName:Ivaro18},
{id:2,fullName:Some-Random Stranger}




$ / code $ / pre
$ b $ p这段代码是基本上和你一样r代码。你现在问的问题是,从我的答案中得到代码,如何获得所有员工的所有ID或姓名。 (获取facet对象的所有键名)



现在让我们在打字稿中给你看看

  someFunction(){
this.http.get('someUrl')。map(response => {
console.log(JSON.stringify(response.json())) ; //检查你实际检索的内容

let decode = response.json();
for(让office.office.OFFICE){
console.log(office.location );

(让office.employees的员工){
console.log(employee.fullName);
}
}
});
}

此程式的预期输出

 >纽约
> Jon Skeet
>随机陌生人
>阿姆斯特丹
> Ivaro18
>一些随机的陌生人

我认为这个伪代码会让你觉得有点偏离并找到你的问题的答案。如果您无法详细说明您希望输出的内容,我会很乐意在任何时候(甚至是周末)为您提供评论,并在我有空时回复!


I find a new problem, I would like to count @NAME to set array FACET[] for show @KEY

Myjson

  "    RESULT":{
              "FACET":[
                { "@NAME" : "creator", 
                  "@COUNT" : "20",
                  "FACET_VALUES":[
                    {
                      "@KEY":"Book Company1",
                      "@VALUE":"13"},
                    {
                      "@KEY":"Book Company1์",
                      "@VALUE":"10"}],
                { "@NAME" : "lang", 
                  "@COUNT" : "70",
                  "FACET_VALUES":[
                    {
                      "@KEY":"tha",
                      "@VALUE":"33"},
                    {
                      "@KEY":"eng",
                      "@VALUE":"42"}
                   ],
                { "@NAME" : "bnb", 
                  "@COUNT" : "64",
                  "FACET_VALUES":[
                    {
                         .
                         .
                         .

              ] 

.

     optionsFn(): void {
            this.http.get("my_url")
                        .subscribe(data =>{
                                 this.date=data.json().RESULT.FACET; //get @NAME
                        },error=>{
err => console.error(err),
                           () => console.log('getRepos completed')
            );
        console.log(this.date);

            console.log(this.date);
            this.goToapply()
      }

      goToapply(){

       this.http.get("my_url")
                        .subscribe(data =>{
                                 this.foundRepos=data.json().RESULT.FACET[0,1,2,3.4......].FACET_VALUES; ///get @KEY
                        },error=>{
                            console.log(error);
                        } );

      }

..

    <ion-label>Filterr</ion-label>
          <ion-select [(ngModel)]="refine" (ionChange)="optionsFn();">
            <ion-option value="..." *ngFor="let item of date">{{item["@NAME"]}},({{item["@COUNT"]}})</ion-option>
          </ion-select>
<ion-list>          
          <ion-item *ngFor="let item of foundRepos" (click)="itemClicked($event,item)">
            <h3> {{ item[@KEY"] }}</h3>
          </ion-item>
        </ion-list>

This value="..." I want to keep it to string for to use, but zi have know idea.

example : count @NAME = 3 (creator,lang,bnb) ,and get value="..." = 0,1,2 (0==creator ,1==lang ,2==bnb) When I select lang I get 1 , I use this to FACET[1].FACET_VALUES , So that I get @KEY

Sorry to write wrong. My english isn't very good.

解决方案

Ok. there's just a tip that I would like to point out to you. You shoud get a bit better with words. Not the English language in particular but more about explaining your current result and your desired result. I believe this will force you into learning english at a much faster pace than you're currently doing. (coming from a non-native speaker)

"I would like to count@NAME to set array FACET[] for show @KEY"

So, with the returned JSON we can do a couple of things, which do not get quite clear from immediately looking at your question (did a +1 since I don't believe it's of bad quality or isn't a good question).

So there are 3 things I should cover in my post

  • Counting the amount that NAME occurs
  • Setting an array for the FACET
  • Showing the KEY per FACET which seems to be the end goal.

Ok

Counting the amount that NAME occurs

So you basically already got this answer. You submitted this code

this.http.get("my_url") .subscribe(data =>{ this.date=data.json().RESULT.FACET; //get @NAME });

Which means you already have access to the FACET array. Counting the amount that NAME occurs can be done either by creating a for loop and checking if the name is not undefined (thus incrementing an int) or counting on the fact that NAME is always defined, thus just calling this.date.size() (if this.date is set correctly of course)

get an array for the FACET

this, you're already doing by assiging the this.date = data.json().RESULT.FACET. Your this.date now contains an array holding your FACET objects. Just see it like this: JSON (with comments which is not possible in JSON but just for demonstration purposes) =

{ FACET: // declare the main object, called FACET 
  [     // declaring the FACET as an Array ([] is for creating a list)
    {id: 2} //initializing a child of the FACET array
    ,{id: 3} //initializing the second FACET child.
  ]
} 

So, this pseudo (not realistic) JSON holds 2 objects, having id = 2 and id = 3

Ok, now you should understand a bit of JSON, let's take a look at how your code looks (taking a Businnes with multiple offices and multiple employees per office into account)

{
  OFFICES" : [
       {
         "id" : "1",
         "location" : "New York",
         "employees" : [
           {"id" : "1", "fullName" : "Jon Skeet"},
           {"id" : "2", "fullName" : "Random Stranger"}
         ]
     },
     {
        "id" : "2",
        "location" : "Amsterdam",
        "employees" : [
            {"id" : "1", "fullName" : "Ivaro18"},
            {"id" : "2", "fullName" : "Some-Random Stranger"}
         ]
      }
  ]
}

This code is basically the same as your code. And the question your asking now is, taking the code from my answer, how to get all the id's or names of all the employees. (get all names of keys of facet objects)

Now let's show you in typescript

someFunction() {
   this.http.get('someUrl').map(response => {
      console.log(JSON.stringify(response.json())); // check what you actually retrieve

      let decoded = response.json();
      for(let office of decoded.OFFICE) {
         console.log(office.location);

         for(let employee of office.employees) {
            console.log(employee.fullName);
         } 
      }
   });
}

Expected output of this program

> New York
> Jon Skeet
> Random Stranger
> Amsterdam
> Ivaro18
> Some-Random Stranger

I think this pseudo code will get you to think a bit out-of-the-box and find the answer to your question. If you can't elaborate more on what you want as output I will be glad to help you at any time (even weekends) just comment and I'll respond when I have the time!

这篇关于如何计算json数据项ionic2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆