1

I know this is rather simple but I'm stuck at it and I'd really like some help

Here's a JSON string I'm generating.

[{"field1":3,"field2":"5","field3":"value3","field4":"value4"},{"field1":3,"field2":"8","field3":"value3","field4":"value4"},{"field1":3,"field2":"6","field3":"value3"}]

How do I extract the value associated with field1?

And how do I access elements of each of the distinct arrays?

3
  • 1
    you'll have to parse the string into an array structure your programming language understands. Which language is that? (javascript, php, ...) Commented Oct 1, 2013 at 13:15
  • Your question is not answerable if you don't tell us which programming language you are using. Commented Oct 1, 2013 at 13:20
  • my bad. I want to use PHP to do this. Commented Oct 1, 2013 at 13:21

7 Answers 7

4

try this code

$jsonString = '[{"field1":3,"field2":"5","field3":"value3","field4":"value4"},{"field1":3,"field2":"8","field3":"value3","field4":"value4"},{"field1":3,"field2":"6","field3":"value3"}]';

$json = json_decode($jsonString, true);

print_r($json);
Sign up to request clarification or add additional context in comments.

Comments

2

Try this (in Javascript)

var jsonData=[{"field1":3,"field2":"5","field3":"value3","field4":"value4"},{"field1":3,"field2":"8","field3":"value3","field4":"value4"},{"field1":3,"field2":"6","field3":"value3"}];

if you get response as text then use jsonData=JSON.parse(yourResponseText);

for(var i=0;i<jsonData.length;i++){
alert('your required val:'+jsonData[i].field1);
}

In php

$data = json_decode($json);//$json is your json data

foreach ($data as $item) {
  echo $item->field1
}

1 Comment

would you be able to help me with a PHP solution?
1

https://stackoverflow.com/a/3627901/485790

console.log(jQuery.parseJSON(' [{"field1":3,"field2":"5","field3":"value3","field4":"value4"},{"field1":3,"field2":"8","field3":"value3","field4":"value4"},{"field1":3,"field2":"6","field3":"value3"}]').field1);

1 Comment

could you help me with doing this in PHP?
1

In php way

$jsonString = '[{"field1":3,"field2":"5","field3":"value3","field4":"value4"},{"field1":3,"field2":"8","field3":"value3","field4":"value4"},{"field1":3,"field2":"6","field3":"value3"}]';

$json = json_decode($jsonString, true);

foreach($json as $item){
   echo $item['field1'];
}

Comments

1

In php you can:

$json = json_decode($_GET['variable'];

http://php.net/manual/en/function.json-decode.php

Comments

1

try this,

var json = '[{"field1":3,"field2":"5","field3":"value3","field4":"value4"},{"field1":3,"field2":"8","field3":"value3","field4":"value4"},{"field1":3,"field2":"6","field3":"value3"}]';
    $.each(jQuery.parseJSON(json), function () {
        alert(this['field1']);
        alert(this['field2']);
        alert(this['field3']);
        alert(this['field4']);

});

Comments

1

this is a simple array of json objects. so with jquery

var jsonArray = JSON.parse("your string");
   for(var int i=0 ; i < jsonArray.length() ; i++){
     var jsonObject = jsonArray[i];
     Console.log(jsonObject.field1)
   }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.