【发布时间】:2015-02-23 05:34:38
【问题描述】:
我的 php 代码是这样的:
<?php
$host="localhost"; //replace with database hostname
$username="root"; //replace with database username
$password=""; //replace with database password
$db_name="hg"; //replace with database name
$con=mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql = "select * from tblquestion";
$result = mysql_query($sql);
$json = array();
if(mysql_num_rows($result)){
while($row=mysql_fetch_assoc($result)){
$json['emp_info'][]=$row;
}
}
mysql_close($con);
echo json_encode($json);
?>
我的输出是这样的:
{
"emp_info": [
{
"QuestionId": "1",
"Question": "Had dinner?",
"TeacherId": "2",
"GradeId": "1",
"SectionId": "",
"SubjectId": "1",
"NofOption": "2",
"COptionId": "1",
"Score": "5",
"isupdated": "0"
},
{
"QuestionId": "2",
"Question": "Give two examples to illustrate Newton’s second law of motion.",
"TeacherId": "11",
"GradeId": "6",
"SectionId": "",
"SubjectId": "48",
"NofOption": "0",
"COptionId": "0",
"Score": "3",
"isupdated": "0"
},
{
"QuestionId": "3",
"Question": "Question.1: Define ‘Balanced’ and ‘Unbalanced’ forces. Illustrate with examples.",
"TeacherId": "11",
"GradeId": "0",
"SectionId": "",
"SubjectId": "0",
"NofOption": "0",
"COptionId": "0",
"Score": "5",
"isupdated": "0"
},
{
"QuestionId": "4",
"Question": "Question.2: What is inertia? How many types of inertia are there?",
"TeacherId": "11",
"GradeId": "6",
"SectionId": "",
"SubjectId": "48",
"NofOption": "0",
"COptionId": "0",
"Score": "3",
"isupdated": "0"
},
{
"QuestionId": "5",
"Question": "Question.3: Define: (a) Momentum, (b) Newton",
"TeacherId": "11",
"GradeId": "6",
"SectionId": "",
"SubjectId": "48",
"NofOption": "0",
"COptionId": "0",
"Score": "3",
"isupdated": "0"
},
{
"QuestionId": "6",
"Question": "Question.4: State the various effects of force. So",
"TeacherId": "11",
"GradeId": "6",
"SectionId": "",
"SubjectId": "48",
"NofOption": "0",
"COptionId": "0",
"Score": "3",
"isupdated": "0"
},
{
"QuestionId": "7",
"Question": "Question.5: State Newton’s first law of motion. Why the Newton’s first law of motion is also called ‘Law of Inertia’?",
"TeacherId": "11",
"GradeId": "6",
"SectionId": "",
"SubjectId": "48",
"NofOption": "0",
"COptionId": "0",
"Score": "4",
"isupdated": "0"
},
{
"QuestionId": "8",
"Question": "Question.6: What is the relation between mass and inertia?",
"TeacherId": "11",
"GradeId": "6",
"SectionId": "",
"SubjectId": "48",
"NofOption": "0",
"COptionId": "0",
"Score": "3",
"isupdated": "0"
},
{
"QuestionId": "9",
"Question": "Question.7: State and explain Newton’s second law of motion.",
"TeacherId": "11",
"GradeId": "6",
"SectionId": "",
"SubjectId": "48",
"NofOption": "0",
"COptionId": "0",
"Score": "3",
"isupdated": "0"
},
{
"QuestionId": "10",
"Question": "Question.9: Explain how Newton’s second law of motion can be used to derive a quantitative definition of Force.",
"TeacherId": "11",
"GradeId": "6",
"SectionId": "",
"SubjectId": "48",
"NofOption": "0",
"COptionId": "0",
"Score": "4",
"isupdated": "0"
},
{
"QuestionId": "11",
"Question": "State Law of Conservation of Momentum. Deduce this from Newton’s second law of motion.",
"TeacherId": "11",
"GradeId": "6",
"SectionId": "",
"SubjectId": "48",
"NofOption": "0",
"COptionId": "0",
"Score": "4",
"isupdated": "0"
},
{
"QuestionId": "12",
"Question": "who is captain of indian cricket team?",
"TeacherId": "3",
"GradeId": "1",
"SectionId": "",
"SubjectId": "1",
"NofOption": "2",
"COptionId": "1",
"Score": "1",
"isupdated": "0"
},
{
"QuestionId": "13",
"Question": "what is your name?",
"TeacherId": "352",
"GradeId": "8",
"SectionId": "",
"SubjectId": "121",
"NofOption": "3",
"COptionId": "1",
"Score": "10",
"isupdated": "1"
},
{
"QuestionId": "14",
"Question": "in which school?",
"TeacherId": "352",
"GradeId": "8",
"SectionId": "",
"SubjectId": "121",
"NofOption": "4",
"COptionId": "3",
"Score": "10",
"isupdated": "1"
},
{
"QuestionId": "15",
"Question": "name",
"TeacherId": "352",
"GradeId": "9",
"SectionId": "",
"SubjectId": "0",
"NofOption": "3",
"COptionId": "1",
"Score": "1",
"isupdated": "1"
},
{
"QuestionId": "16",
"Question": "meeting",
"TeacherId": "352",
"GradeId": "9",
"SectionId": "",
"SubjectId": "0",
"NofOption": "4",
"COptionId": "3",
"Score": "1",
"isupdated": "1"
},
{
"QuestionId": "17",
"Question": "do u want to go home?",
"TeacherId": "2",
"GradeId": "1",
"SectionId": "",
"SubjectId": "1",
"NofOption": "2",
"COptionId": "2",
"Score": "1",
"isupdated": "0"
}
]
}
现在我想在 android 中单独检索每个 questionId(即 question、teacherId、grade 等)数据。 例如: questionId:1,问题:吃过晚餐,teacherId:2 ... questionId:2,问题:举两个例子,teacherId:11 ... questionId:3,问题:定义,teacherId:11 ...
【问题讨论】:
-
您在解析 Json 时遇到问题吗?
-
每个值都有问题ID吗?至于Android部分,你有什么问题?你能解析json吗?
-
@Tushar 是的,我在解析 json 时遇到问题,是的,每个问题都有不同的问题 ID