Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
115 views
Welcome To Ask or Share your Answers For Others

1 Answer

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...