1

I have the following code to extract values from a JSON response. What I am trying to do is store the data in a similar way to how you would with an associative array in php. Apologies for the code being inefficient. The array comments written down are how I would like it to look in the object.

$.each(responseData, function(k1,v1){
            if(k1 == "0"){
                $.each(v1, function(k2,v2){
                    $.each(v2, function(k3, v3){
                        if(k3 == "val"){
                          //store in object here
                          //Array1 = array("time"=>k2, "iVal"=>v3)
                          console.log(k3 + v3 + k2);
                        }else{
                           //Array2 = array("time"=>k2, "aVal"=>v3)
                           console.log(k3 + v3 + k2);
                        }
                    });
                });
            }
});

So all the information is there but I am not sure how to store each instance for the values in an object. I did try store it like this:

//obj created outside

obj1.date = k2;
obj2.iVal = v3;

But doing this clearly overwrote every time, and only kept the last instance so I am wondering how can I do it so that all values will be stored?

Edit: Added input and output desired.

Input

{"0":{"18.00":{"iVal":85.27,"aVal":0.24},"19.00":{"iVal":85.27,"aVal":0.36},"20.00":{"iVal":0,"aVal":0}}, "success":true}

Desired output

array1 = {"time":"18.00", "iVal":85.27},{"time":"19.00", "iVal":85.27},{"time":"20.00", "iVal":0}
array2 = {"time":"18.00", "aVal":0.24},{"time":"19.00", "aVal":0.36},{"time":"20.00", "aVal":0}
2
  • Can you supply sample input and desired output ? (instead of all this code) ? Commented Mar 13, 2014 at 15:06
  • @RoyiNamir Edited it please have a look. My indexing for the desired output could be incorrect as I am not too clear with javascript objects. Commented Mar 13, 2014 at 15:18

2 Answers 2

1

try this :

var g1=[];
var g2=[];


for ( a in o[0])
  {
    g1.push({time:a , iVal:o[0][a]['iVal']})
    g2.push({time:a , aVal:o[0][a]['aVal']})

}

http://jsbin.com/qividoti/3/edit

enter image description here

Sign up to request clarification or add additional context in comments.

7 Comments

I just tried a fiddle for this with my returned data and worked fine but when I try it in my application it gives an undefined result, when I returning encoded JSON data and using it as the object do I have to do anything different?
@user3144542 can u supply the json ?
The JSON when outputted to my log is exactly the same as the input above. Unless you mean something different?
I did use your json 1:1. so where is the problem ?
@user3144542 can you supply the ' it gives a different result'
|
0

a json response can be converted back to a js object literal by calling JSON.parse(jsonString) inside the success callback of your ajax call.

from then on there is no need for iterating over that object since you navigate it like any other js object which is can be done in two ways either

the js way -> dot notation

var obj = JSON.parse(jsonStirng);
var value = obj.value;

or like a php array

var value = obj["value"];

2 Comments

I can't do that because the value will not be known and is dynamically please see the edit which may clear things up.
the value is a placeholder for whatever 'value' you want to use ! in fact it is the key of the array or object which could also be called object.property

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.