user3361865
user3361865

Reputation: 1

how to read this simple json?

I have this json (from file Php):

{
    "rows": [
        {
            "id": "91",
            "cell": [
                "91",
                "Europe",
                "England",
                "Mark",
                "London",
                "blablabla"
            ]
        }
    ]
}

I must read it by jquery. I use this code incomplete:

$.ajax({
  url: 'test.php',
  dataType: 'json',
  data: data,
  success: function(response){      
       var obj = JSON.parse(response);
       ?????
  }
}); 

Example output:

State: Europe
Name : Mark
City: London
Note: Blablabla

Json return always only row. I have found many examples but the format that I use is very different.

Upvotes: 0

Views: 54

Answers (2)

hemanth
hemanth

Reputation: 1043

You access elements in array by index. In your response Rows and cell are Arrays.

For state = `json_response.rows[0].cell[1]` //Index of state is 1 in `cell`
City: `json_response.rows[0].cell[4]`

Note: If it always returns one row you can limit our response just to

    {
        "id": "91",
        "cell": [
            "91",
            "Europe",
            "England",
            "Mark",
            "London",
            "blablabla"
        ]
    }

fiddle

Upvotes: 0

Igor
Igor

Reputation: 33983

The same way you would any object:

obj.rows
obj.rows[0].id
obj.rows[0].cell
obj.rows[0].cell[0]
//etc.

Obviously you should create a variable for any values you use more than once, but for clarity's sake, I've excluded that here.

Upvotes: 1

Related Questions