java – org.json.JSONArray无法转换为JSONObject
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – org.json.JSONArray无法转换为JSONObject,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3282字,纯文字阅读大概需要5分钟。
内容图文
我是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全部内容,希望文章能够帮你解决java – org.json.JSONArray无法转换为JSONObject所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。