简体   繁体   English

如何计算json数据项ionic2

[英]How to count json data item ionic2

I find a new problem, I would like to count @NAME to set array FACET[] for show @KEY我发现了一个新问题,我想算一下@NAME来设置数组@NAME FACET[]以显示@KEY

Myjson 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.这个value="..."我想把它保留为string以供使用,但zi知道这个想法。

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示例:count @NAME = 3 (creator,lang,bnb) ,并获得value="..." = 0,1,2 (0==creator ,1==lang ,2==bnb)当我选择lang我得到1 ,我用它来FACET[1].FACET_VALUES ,所以我得到@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" “我想计数@NAME 来设置数组 FACET[] 以显示 @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).因此,使用返回的 JSON,我们可以做一些事情,但从立即查看您的问题时并不能很清楚(做了一个 +1,因为我不认为它的质量很差或不是一个好问题)。

So there are 3 things I should cover in my post所以我应该在我的帖子中介绍 3 件事

  • Counting the amount that NAME occurs计算NAME出现的数量
  • Setting an array for the FACET为 FACET 设置数组
  • Showing the KEY per FACET which seems to be the end goal.显示每个 FACET 的 KEY,这似乎是最终目标。

Ok好的

Counting the amount that NAME occurs计算NAME出现的数量

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.这意味着您已经可以访问 FACET 数组。 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)计数该NAME发生可以或者通过for循环创建和检查,如果名称没有未定义的(因而递增一个int)上NAME总是被定义的事实或计数,从而只调用完成的量this.date.size() (当然,如果 this.date 设置正确)

get an array for the FACET获取 FACET 的数组

this, you're already doing by assiging the this.date = data.json().RESULT.FACET .这个,你已经通过分配this.date = data.json().RESULT.FACET Your this.date now contains an array holding your FACET objects.您的this.date现在包含一个保存 FACET 对象的数组。 Just see it like this: JSON (with comments which is not possible in JSON but just for demonstration purposes) =就像这样看:JSON(带有在 JSON 中不可能的注释,但仅用于演示目的)=

{ 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所以,这个伪(不现实的)JSON 包含 2 个对象, id = 2id = 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)好的,现在你应该对 JSON 有点了解了,让我们来看看你的代码是什么样子的(考虑到一个有多个办公室和每个办公室多个员工的 Businnes)

{
  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.你现在问的问题是,从我的答案中获取代码,如何获取所有员工的所有 ID 或姓名。 (get all names of keys of facet objects) (获取 facet 对象的键的所有名称)

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!如果您不能详细说明您想要的输出内容,我很乐意随时(甚至是周末)为您提供帮助,只需发表评论,我会在有时间的时候回复您!

@Ivaro18 thanks for your answers. @Ivaro18 感谢您的回答。 I want to counting the amount that @NAME for setting an array for the FACET and showing the KEY per FACET[] which seems to be the end goal.我想计算@NAME@NAME设置数组并显示每个FACET[]的 KEY 的数量,这似乎是最终目标。

Yes, you totally know what I meant.是的,你完全明白我的意思。 But i want output is every @KEY of that FACET[]但我希望输出是该FACET[]每个@KEY

Example :例子 :

<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>

. .

 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);
                        } );

      }

from above ion-select is show creator , lang and bnb ,I want to If When I select lang ion-option value="..." to keep number of "@NAME"从上面 ion-select 是 show creatorlangbnb ,我想如果当我选择lang ion-option value="..."来保留"@NAME" number

  • example counting amount of @NAME is 3 and @NAME示例计数量为 3 和
  • when I select creator is ion-option value="..." << is 0当我选择creator是 ion-option value="..." << is 0
  • when I select lang is ion-option value="..." << is 1当我选择lang is ion-option value="..." << is 1
  • when I select bnb is ion-option value="..." << is 2当我选择bnb是 ion-option value="..." << is 2

and If i get value ,I take it to goToapply() for set FACET[]如果我得到了value ,我会把它带到goToapply()来设置goToapply() FACET[]

  • example I select bnb I get value =2 and console.log this.refine is show 2例如我选择bnb我得到值 =2 和console.log this.refine是显示2
  • take it(2) to let p = this.refine , so that p = 2把它(2) let p = this.refine ,这样p = 2
  • take p to this.foundRepos=data.json().RESULT.FACET[p].FACET_VALUES;将 p 带到this.foundRepos=data.json().RESULT.FACET[p].FACET_VALUES; for show @KEY in ion-list用于在ion-list显示@KEY

When I select lang output is当我选择lang输出时

> tha
> eng

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

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