家伙不重复请致使我的json
开头:
[
{
}
]
没有根元素。我不知道如何解析这个作为你的答案。我是php的新手。目前开发一个Android应用程序,发送一个json字符串到PHP ..我的json数据是字符串格式。
我怎么能在php中解析这个字符串。我需要此字符串中的每个id
和name
存储在数据库中...
[
{"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"
}
]
答案 0 :(得分:2)
首先你的照片值不正确它的值应该是
"photo":"http://........./diary\/photos\/student\/26.png"
或
"photo":"http:/diary\/photos\/student\/26.png"
或其他不像http:\....
如果它的值是固定的那么它的解决方案是
$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;
}
答案 1 :(得分:1)
试试这个:
$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
}
答案 2 :(得分:1)
解析
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();
}
解析并将其添加到数组后。使用相同的方法填充您的微调器。
[
表示json数组节点
{
表示json对象节点
答案 3 :(得分:1)
要迭代多维数组,可以使用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";
}
}
输出:
John:
status => Not Active
Jennifer:
status => Active
James:
status => Active
age => 52
count => 14
progress => 29857
bad => 10