Json result multiple nested value cannot parse

nativescriptcore
android

#1
{
   "type": "ValueChanged",
   "key": "textbox",
   "value": {
     "-LKUbR02AkIicH69WwX8": {
       "text": "Hello"
     },
     "-LKUbXhyl373DIgIW_XC": {
       "text": "Hello 123"
     }
   }
 }

i know result['value'] will print only values,

but how to print only text ?

result['value'].text was undefined


#2

It’s as simple as result.value["-LKUbR02AkIicH69WwX8"].text, basics of JavaScript / JSON object.


#3

no i have more than 100 data in table. and each have different id. i want to display all text and list in xml. so i cant give id. and im getting these json object from firebase.


#4

Maybe this will help you.(second example)
https://www.codexpedia.com/javascript/javascript-loop-through-array-and-object-properties/
But instead of doing console.log you may create simple array, and push JSON’s elements into it.


#5

your link will work only for html and javascript. it said key is undefined.

everyone was using firebase & no one was faced issues i got. and im trying to do a simple crud operation that is.

i am storing data to firebase using

var text = viewModel.get("text");  
// this creates a unique id and stores the value
      firebase.push(
        '/textbox',
        {
          'text': text,
        }
            ).then(
                function (result) {
                console.log("created key: " + result.key);
                }
        );

now im retreiving it by

firebase.getValue(val)
    .then(function(result){
        console.log(result);
        console.log(JSON.stringify(result));
    }

output of result console

{
   "type": "ValueChanged",
   "key": "textbox",
    "value": {
     "-LK_J389ffLEoxWKDncv": {
       "text": "Welcome"
     },
     "-LK_IytD9D_zetMyflm6": {
       "text": "Hello"
     }
   }
 }

output of stringify result console

!......this i altered for better visual.......
 {
   "type":"ValueChanged","key":"textbox",
   "value":{
          "-LK_J389ffLEoxWKDncv":{"text":"Welcome"},
         "-> > LK_IytD9D_zetMyflm6":{"text":"Hello"}
            }
  }

i have a list view like this

<ListView row="1" class="list-group" items="{{ textList }}" itemTap="{{ onItemTap }}" style="height:1250px">
				<ListView.itemTemplate>
					<FlexboxLayout flexDirection="row" class="list-group-item">
						<Label text="{{ text }}" class="list-group-item-heading" verticalAlignment="center" style="width: 60%" />
					</FlexboxLayout>
				</ListView.itemTemplate>
</ListView>

all i want is to retrive the text field from result and show it in list.

please help me on this …


#6

your link will work only for html and javascript. it said key is undefined.

everyone was using firebase & no one was faced issues i got. and im trying to do a simple crud operation that is.

i am storing data to firebase using

var text = viewModel.get("text");  
// this creates a unique id and stores the value
      firebase.push(
        '/textbox',
        {
          'text': text,
        }
            ).then(
                function (result) {
                console.log("created key: " + result.key);
                }
        );

now im retreiving it by

firebase.getValue(val)
    .then(function(result){
        console.log(result);
        console.log(JSON.stringify(result));
    }

output of result console

{
   "type": "ValueChanged",
   "key": "textbox",
    "value": {
     "-LK_J389ffLEoxWKDncv": {
       "text": "Welcome"
     },
     "-LK_IytD9D_zetMyflm6": {
       "text": "Hello"
     }
   }
 }

output of stringify result console

!......this i altered for better visual.......
 {
   "type":"ValueChanged","key":"textbox",
   "value":{
          "-LK_J389ffLEoxWKDncv":{"text":"Welcome"},
         "-> > LK_IytD9D_zetMyflm6":{"text":"Hello"}
            }
  }

i have a list view like this

<ListView row="1" class="list-group" items="{{ textList }}" itemTap="{{ onItemTap }}" style="height:1250px">
				<ListView.itemTemplate>
					<FlexboxLayout flexDirection="row" class="list-group-item">
						<Label text="{{ text }}" class="list-group-item-heading" verticalAlignment="center" style="width: 60%" />
					</FlexboxLayout>
				</ListView.itemTemplate>
</ListView>

all i want is to retrive the text field from result and show it in list.

please help me on this …


#7

@Konrad had posted exactly what you need to do here, there is nothing specific to Browser or Html in that link.

If you don’t mind I would highly recommend you to spend quite good time in the basic concepts of JavaScript, then you won’t be stuck again.


#8

Hi @vikas,

Maybe this might be helpful:

var yourData = {
        "type": "ValueChanged",
        "key": "textbox",
        "value": {
            "-LKUbR02AkIicH69WwX8": {
                "id":'1',
                "text": "Hello"
            },
            "-LKUbXhyl373DIgIW_XC": {
                "id":'2',
                "text": "Hello 123"
            }
        }
    };

    var oldIndex = new Array;
    var newIndex = new Array;
    Object.keys(yourData).forEach(function (key) {
        oldIndex.push(yourData[key])
    });
    var final = oldIndex[2];
    Object.keys(final).forEach(function (key) {
        newIndex.push(final[key]['text'])
    });

    // get all texts
    console.log(newIndex);
    // get first text
    console.log(newIndex[0]);
    // get type
    console.log(typeof newIndex);
    // see content
    console.dir(newIndex);
    // You may want do add ID, and then you can search array:
    var arrFormObj = Object.values(final)
    var result = arrFormObj.find(el => el.id == '2');
    console.log(result);

#9

thank you very much for your valuable reply. i played with it little and came upto this.

 var resval = result.value
 var arrdata = [];

 Object.keys(resval).forEach(function(key){
           arrdata.push(resval[key]);
           })
  
  console.log(arrdata)    
  viewModel.item = arrdata;

so in one foreach loop only i can get the result.


#10

No worries.
All i’m doing is - im paying my bills to the Community, trying to help as @manojdcoder is doing this every day.
Cheers @manojdcoder


#11

BTW : @manojdcoder, do you have birthday today ?
image
if yes, All the best Bro !:slight_smile:


#12

yes i seen every isuues and every where he is the one replying to most of the questions. he his really crazy and best…


#13

Thank you :slight_smile: