-1

Hi friends I have a I have a json string as shown below. How to parse the string to get day,min_amount,max_amount values .

[{"day":"1970-01-01","min_amount":"0.00","max_amount":"0.00"},{"day":"1970-01-02","min_amount":"1.00","max_amount":"2.00"}]
Rishi Reddy
  • 123
  • 1
  • 2
  • 10

3 Answers3

1

Just use JSON.parse. The syntax for accessing a value is simple:

obj = JSON.parse(json)
day = obj[0].day
min_amount = obj[0].day
max_amount = obj[0].day

The great thing about Javascript is how simple it is to use JSON, because JSON is just a serialized version of plain-old javascript hashes, arrays, and scalars.

dsw88
  • 4,400
  • 8
  • 37
  • 50
0

It's already in object form. SO use this :

var x = [{"day":"1970-01-01","min_amount":"0.00","max_amount":"0.00"},{"day":"1970-01-02","min_amount":"1.00","max_amount":"2.00"}]
jQuery.each(x,function(e){
    console.log(x[e])
     console.log(x[e].day)
});

Here is the working example : http://jsfiddle.net/u6J8A/

Nishu Tayal
  • 20,106
  • 8
  • 49
  • 101
0

As you may not have noticed, JSON is JavaScript synthax.

<script type="text/javascript">
    var data = [
        {"day":"1970-01-01","min_amount":"0.00","max_amount":"0.00"},
        {"day":"1970-01-02","min_amount":"1.00","max_amount":"2.00"}
    ];
</script>

Dumping it directly in the JavaScript code is perfectly valid.

But if you are fetching this data at run time and have the information as a string, you can convert it using JSON.parse(string).

The information can be then read from this structure by the variables data[0].day, data[0].min_amount, data[0].max_amount, data[1].day, data[1].min_amount, data[1].max_amount.

Havenard
  • 27,022
  • 5
  • 36
  • 62