我该如何解析这种JSON?

时间:2017-05-23 00:02:25

标签: android json

我是JSON解析的新手。如果有人帮我解析Android中的这种json数组,那将是一个很大的帮助。 谢谢

{
  "response": 200,
  "department": [
    "Information Technology"
  ],
  "subject": [
    "ads(th)"
  ],
  "professional": [
    "cg(th)",
    "cg(lab)"
  ],
  "semester": [
    "3A",
    "5A",
    "5A"
  ]
}

4 个答案:

答案 0 :(得分:1)

这是你的答复:

{
  "response": 200,
  "department": [
    "Information Technology"
  ],
  "subject": [
    "ads(th)"
  ],
  "professional": [
    "cg(th)",
    "cg(lab)"
  ],
  "semester": [
    "3A",
    "5A",
    "5A"
  ]
}

你可以这样做

String responseString="" //this string is ur web service response
try {
        //JSON is the JSON code above

        JSONObject jsonResponse = new JSONObject(responseString);
        JSONArray department = jsonResponse.getJSONArray("department");
        String hey = department.toString();

JSONArray subject = jsonResponse.getJSONArray("subject");
        String sub = subject.toString();

JSONArray  professional= jsonResponse.getJSONArray("professional");
        String pro = professional.toString();
//like this u can parse other JsonArray

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

在jsonarray中获取这些值之后,你想在spinner中显示它,而不是你可以这样做

ArrayList<String> listdata = new ArrayList<String>();  
if (professional != null) { 
   for (int i=0;i<professional.length();i++){ 
    listdata.add(professional.getString(i));
   } 
} 

用于显示微调器

Spinner spinner = (Spinner) findViewById(R.id.SpinnerSpcial);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listdata);//Pass list data of Profession
spinner.setAdapter(adapter);

备注: 您也可以通过扩展BaseAdapter或ArrayAdapter for Spinner来制作自定义适配器。

希望这会对你有所帮助......如果你有任何疑问,你可以问

答案 1 :(得分:1)

试试这个,

try {
    JSONObject obj_result=new JSONObject(result);
    String response=obj_result.getString("response");

    JSONArray arr_department=obj_result.getJSONArray("department");
    for(int i=0;i<arr_department.length();i++)
    {
        String  department_name=arr_department.getString(i);
        Log.d("TAG","department_name:"+department_name);
    }

    JSONArray arr_subject=obj_result.getJSONArray("subject");
    for(int i=0;i<arr_subject.length();i++)
    {
        String  subject_name=arr_subject.getString(i);
        Log.d("TAG","subject_name:"+subject_name);
    }

    JSONArray arr_professional=obj_result.getJSONArray("professional");
    for(int i=0;i<arr_professional.length();i++)
    {
        String  professional_name=arr_professional.getString(i);
        Log.d("TAG","professional_name:"+professional_name);
    }

    JSONArray arr_semester=obj_result.getJSONArray("semester");
    for(int i=0;i<arr_semester.length();i++)
    {
        String  semester_name=arr_semester.getString(i);
        Log.d("TAG","semester_name:"+semester_name);
    }

} catch (JSONException e) {
    e.printStackTrace();
}

答案 2 :(得分:0)

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Title</title>
    <link href="styles.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<header> Welcome to Toner Stock </header>
<h1>Add Buyer</h1>
<div id="mynav" align="center">
    <ul>
        <li><a href="/">Home</a></li>
        <li><a href="add-buyer">Add Buyer</a></li>
        <li><a href="add-manager">Add Manager</a></li>
        <li><a href="current-stock">Current Stock</a></li>
        <li><a href="transactions">Transactions</a></li>
        <li><a href="orders">Order Form</a></li>
    </ul>
</div>

    <div id="display-table" align="center">
       <form th:action="@{/addBuyer}" th:object="${buyer}" style="width:100%" method="post">
           <table>
               <td><label>First Name: </label></td>
               <td><input type="text" th:field="*{firstName}"/></td>
               <td><label>Last Name: </label></td>
               <td><input type="text" th:field="*{lastName}"/></td>
               <td><label>Enter Address: </label></td>
               <td><input type="text" th:field="*{buyerAddress}"/></td>
               <td><input type="submit" value="save"/></td>
           </table>
       </form>
    </div>
<div>
    <div>
        <table id="info-table" align="center" border="1">
            <tr>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Address</th>
            </tr>
            <tr th:each="buyer : ${buyers}">
                <td th:text="${buyer.firstName}"></td>
                <td th:text="${buyer.lastName}"></td>
                <td th:text="${buyer.buyerAddress}"></td>
                <td>
                    <form th:action="@{/deleteBuyer}" th:object="${buyer}" method="post">
                        <input type="hidden" name="buyerid" id="buyerid" value="${buyer.buyerId}"/>
                        <input type="submit" value="Delete" onClick="return confirm('sure?')"/>
                    </form>
                </td>
            </tr>
        </table>
    </div>
</div>
    <div>
        <select>
            <option th:each="buyer : ${buyers}"
                    th:text="${buyer.firstName}"
                    th:value="${buyer.buyerId}"
            ></option>
        </select>
    </div>
<div>
    <div>
    </div>
</div>

</body>
</html>

答案 3 :(得分:0)

我建议您使用Gson Libray

使用Gson,您的映射类将如下所示:

public class YourClass {

@SerializedName("response")
private Integer response;

@SerializedName("department")
private List<String> department;

@SerializedName("subject")
private List<String> subject;

@SerializedName("professional")
private List<String> professional;

@SerializedName("semester")
private List<String> semester;
}

使用Gson,您可以轻松地将您的Json转换为POJO(反之亦然)。查看文档。