java - org.json.JSONArray 无法转换为 JSONObject

标签 java android json

我是 JSON 的新手,我遇到了以下异常:

org.json.JSONArray 无法在 try 部分本身的第一行中转换为 JSONObject

请帮我删除这个。这是我的代码:

try {   
    JSONObject json = new JSONObject(strResponse);

    //Get the element that holds the internship ( JSONArray )
    JSONArray name = json.names();
    JSONArray  internships = json.toJSONArray(name);

    //Loop the Array
    for(int i=0;i < internships.length();i++) {     
        Log.e("Message","loop");
        HashMap<String, String> map = new HashMap<String, String>();
        JSONObject e = internships.getJSONObject(i);
        map.put("id",  String.valueOf("id"));
        map.put("title", "Title :" + e.getString("title"));
        map.put("company", "Company : " +  e.getString("company"));
        map.put("category", "Category : " +  e.getString("category"));
        mylist.add(map);
    } 
} catch(JSONException e) {
    Log.e("log_tag", "Error parsing data "+e.toString());
}

这是我从我的 php 文件中获取的 json

[
 {
    "id": "31",
    "title": "Business Development - Executive",
    "company": "Indidelights",
    "category": "Sales and Business Development"
 },
 {
    "id": "40",
    "title": "Business Development - Ecommerce MH",
    "company": "Ram Gopal & Co",
    "category": "Sales and Business Development"
 },
 {
    "id": "41",
    "title": "Sales and Business development intern",
    "company": "Esanchalak",
    "category": "Sales and Business Development"
 },
 {
    "id": "42",
    "title": "Purchase Executive",
    "company": "Winni.in",
    "category": "Marketing"
 },
 {
    "id": "43",
    "title": "Marketing Intern",
    "company": "Walkover Web Solutions Pvt. Ltd.",
    "category": "Marketing"
 },
 {
    "id": "44",
    "title": "Marketing Intern",
    "company": "SkillKindle Learning Pvt Ltd",
    "category": "Marketing"
 },
 {
    "id": "45",
    "title": "Graphic Designer",
    "company": "Stylopa",
    "category": "Graphic Design / Art Work"
 },
 {
    "id": "46",
    "title": "Graphic Designer",
    "company": "LycondonFX",
    "category": "Graphic Design / Art Work"
 },
 {
    "id": "47",
    "title": "Web Designer",
    "company": "Xapify LLC",
    "category": "Software"
 },
 {
    "id": "48",
    "title": "Web Designer (Frontend)",
    "company": "gotrademark.in",
    "category": "Web Design and Development"
 },
 {
    "id": "49",
    "title": "Content Writing Intern",
    "company": "National Entrepreneurship Network",
    "category": "Content Writing / Journalism"
 },
 {
    "id": "50",
    "title": "Content Writing Intern",
    "company": "Pragmatum Training Pvt Ltd",
    "category": "Content Writing / Journalism"
 },
 {
    "id": "51",
    "title": "HR Intern",
    "company": "GATI Kintetsu Express Pvt Ltd",
    "category": "HR / Recruitment"
 },
 {
    "id": "52",
    "title": "Pharma Intern",
    "company": "Qlinics Health Care Pvt Ltd",
    "category": "BioTechnology / Pharma"
 },
 {
    "id": "53",
    "title": "Android Developer",
    "company": "InoXapps Mobile Solutions Pvt Ltd",
    "category": "Mobile App Development"
 },
 {
    "id": "54",
    "title": "Mobile App developer",
    "company": "RV Media Inc",
    "category": "Mobile App Development"
 },
 {
    "id": "55",
    "title": "Electronics Intern",
    "company": "GA SOFTWARE TECHNOLOGIES PVT LTD",
    "category": "Electronics Engineering"
 }
 ]

最佳答案

这个

JSONObject json = new JSONObject(strResponse);
// your strResponse is a json array 

应该是

JSONArray jsonarray = new JSONArray(strResponse);

[代表json数组节点

{代表json对象节点

for(int i=0; i < jsonarray.length(); i++) {
    JSONObject jsonobject = jsonarray.getJSONObject(i);
    String id       = jsonobject.getString("id");
    String title    = jsonobject.getString("title");
    String company  = jsonobject.getString("company");
    String category = jsonobject.getString("category");
}

关于java - org.json.JSONArray 无法转换为 JSONObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17441246/

相关文章:

java - 知名类型的 android nfc intent-filter

java - 使用 webdriver 查看元素是否存在而不抛出异常?

android - 按不在结果集中的术语排序

android - fragment 共享元素转换不适用于 ViewPager

database - 在数据库和 Json 中保存新行

javascript - PHP/JavaScript : how to pass array from PHP to JavaScript

java - 有什么方法可以使用 JAX-WS 从 Javadoc 生成 WSDL 文档?

java - JavaFX 中的磨砂玻璃效果?

android - 确定移动设备的最大浏览器高度(本地址栏未显示时)

javascript - 递归循环 JavaScript 后使用自定义 HTML 元素作为 JSON 键变量