-3

Guys its not duplicate please cause my json start with:

[
 {

 }
]

without root element. I don't know how to parse this as your answers. I'm new to php. currently developing a android app that send a json string to php.. My json data is in format of string. How can I parse this string in php. I need every id and name from this string to store in database...

[
 {"name":"Abhishek Singh",
  "photo":"http:\........./diary\/photos\/student\/26.png",
  "rollno":"1",
  "id":"26"},
 {"name":"Ashutosh Tripathi",
  "photo":"http:\........./diary\/photos\/student\/34.png",
  "rollno":"2",
  "id":"34"},
 {"name":"Ayushi Srivastava",
  "photo":"http:\............./diary\/photos\/student\/42.png",
  "rollno":"3",
  "id":"42"
 }
]
Maveňツ
  • 1
  • 12
  • 50
  • 89
Abhishek Singh
  • 9,008
  • 5
  • 28
  • 53
  • try to use json_decode you can see it's details at http://php.net/manual/en/function.json-decode.php – Abbas Jan 10 '17 at 05:12
  • bro I am android developer so i don't know how to get that data in $var.. just little help needed how to get id from that – Abhishek Singh Jan 10 '17 at 05:17
  • 1
    @AbhishekSingh: Post it with PHP tags will get better response – ρяσѕρєя K Jan 10 '17 at 05:19
  • 2
    Possible duplicate of [How to parse JSON Array (Not Json Object) in Android](http://stackoverflow.com/questions/18977144/how-to-parse-json-array-not-json-object-in-android) – Vishal Chhodwani Jan 10 '17 at 06:05
  • 1
    I think this is a valid question. I remember years back when I started working with json and I only had a little over a year's experience in php, working with json can get super confusing ... I say leave the question here... okay you guys hav valid points.... – pythonian29033 Jan 10 '17 at 08:51
  • @VishalChhodwani i did'nt ask for parsing in android.. i don't know why downvoting – Abhishek Singh Jan 27 '17 at 09:03
  • There is my +1 on the question even after flagging it the duplicate. – Vishal Chhodwani Jan 27 '17 at 09:15

4 Answers4

2

First of all your photo value is not correct it's value should be like

"photo":"http://........./diary\/photos\/student\/26.png"

or

"photo":"http:/diary\/photos\/student\/26.png"

or some thing else not like http:\....

And if it's value is fixed then it's solution is

$json = '[{"name":"Abhishek Singh",
  "photo":"http://........./diary\/photos\/student\/26.png",
  "rollno":"1",
  "id":"26"},
 {"name":"Ashutosh Tripathi",
  "photo":"http://........./diary\/photos\/student\/34.png",
  "rollno":"2",
  "id":"34"},
 {"name":"Ayushi Srivastava",
  "photo":"http://............./diary\/photos\/student\/42.png",
  "rollno":"3",
  "id":"42"}]';

$obj = json_decode($json);
foreach ($obj as $key=>$record){
    echo $record->id;
}
Abbas
  • 763
  • 1
  • 12
  • 26
1

Try this:

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

foreach($data as $key=>$value){
print_r($value);// this should print each array of json
}

DEMO

Suchit kumar
  • 11,809
  • 3
  • 22
  • 44
1

To parse

try {

   JSONArray jr = new JSONArray("string_name");
   JSONObject jb = (JSONObject)jr.getJSONObject(0);
   JSONArray st = jb.getJSONArray("key");
   for(int i=0;i<st.length();i++)
       {
      String id= st.getString(i);
      String name= st.getString(i+1);
      Log.i("..........",""+key);
      // loop and add it to array or arraylist
        }
   }catch(Exception e)
   {
        e.printStackTrace();
   }

Once you parse and add it to array. Use the same to populate your spinner.

[ represents json array node

{ represents json object node

Maveňツ
  • 1
  • 12
  • 50
  • 89
1

To iterate over a multidimensional array, you can use RecursiveArrayIterator

$jsonIterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator(json_decode($json, TRUE)),
    RecursiveIteratorIterator::SELF_FIRST);

foreach ($jsonIterator as $key => $val) {
    if(is_array($val)) {
        echo "$key:\n";
    } else {
        echo "$key => $val\n";
    }
}

Output:

John:
status => Not Active
Jennifer:
status => Active
James:
status => Active
age => 52
count => 14
progress => 29857
bad => 10
Maveňツ
  • 1
  • 12
  • 50
  • 89