Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android json异常类型不匹配_Android_Json - Fatal编程技术网

android json异常类型不匹配

android json异常类型不匹配,android,json,Android,Json,我的应用程序在json上运行得非常好 { "id":"1", "title_en":"Civil War", "artist_en":"MOTORHEAD" } 但是当我尝试添加多首这样的歌曲时 { "song_list":[ {"id":"1", "title_en":"Civil War", "artist_en":"MOTORHEAD"}, {"id":"2", "title_en":"Slide It In", "artist_en":"

我的应用程序在json上运行得非常好

{
"id":"1",
"title_en":"Civil War",
"artist_en":"MOTORHEAD"
}
但是当我尝试添加多首这样的歌曲时

{
"song_list":[
    {"id":"1",
    "title_en":"Civil War",
    "artist_en":"MOTORHEAD"},
    {"id":"2",
    "title_en":"Slide It In",
    "artist_en":"WHITESNAKE"}]
}
即使在检索数据之前,它也会生成一个异常

09-08 07:29:53.998: W/System.err(982): org.json.JSONException: Value ? of type java.lang.String cannot be converted to JSONObject
09-08 07:29:53.998: W/System.err(982):  at org.json.JSON.typeMismatch(JSON.java:107)
09-08 07:29:53.998: W/System.err(982):  at org.json.JSONObject.<init>(JSONObject.java:158)
09-08 07:29:53.998: W/System.err(982):  at org.json.JSONObject.<init>(JSONObject.java:171)
09-08 07:29:53.998: W/System.err(982):  at com.dwaik.jsonparser.JSONParser.getJSONObject(JSONParser.java:46)
09-08 07:29:53.998: W/System.err(982):  at com.dwaik.myapp.CustomizedListView.onCreate(CustomizedListView.java:48)
以及它的号召

final JSONObject jObject = (new JSONParser(this)).getJSONObject(getResources().openRawResource(R.raw.sample));

快速浏览一下,您试图将整个字符串转换为JsonObject,但是,它不是JsonObject,实际上是一个包含多个JsonObject的JsonArray

下面是一个小代码片段,用于指导您完成解析包含json数组的json字符串的类似过程:

public ArrayList<Ride> parse (String str) throws JSONException 
{ 
    ArrayList<Ride> rides = new ArrayList<Ride>(); 


    try{ 
        JSONArray ridesJsonArray = new JSONArray(str); 

        Ride ride; 
        for (int i=0;i<ridesJsonArray.length();i++) 
        { 
            ride = new Ride(); 
            JSONObject o = ridesJsonArray.optJSONObject(i); 
            ride.rideId=o.optInt("id"); 
            ride.fullName=o.optString("fullname"); 
            ride.origin=o.optString("origin");               
            ride.destination=o.optString("destination"); 
            ride.date=o.optString("date"); 
            ride.time=o.optString("time"); 
            ride.phone=o.optString("phone"); 
            ride.email=o.optString("email"); 
            ride.comments=o.optString("comments"); 

            //log message to make sure the rides are coming from the server 
            Log.i("Rides", ride.fullName + " | "); 

            rides.add(ride);             
        } 
        return rides; 
        }                                                
    catch (Exception e) { 
        e.printStackTrace(); 
        return null; 
    } 
} 
public ArrayList parse(String str)抛出JSONException
{ 
ArrayList rides=新建ArrayList();
试试{
JSONArray=新JSONArray(str);
骑乘;

对于(inti=0;idoesnt),这里的JSON根元素表示一个对象,并在其中包含一个数组?对不起,我是新手JSON@Warlock
{
表示json对象节点。
[
表示一个json数组节点。您得到的是一个字符串而不是json对象。因此出现了错误。请检查您发送的json数据。我使用json验证程序发现了问题,有一些“و”而不是“,”…键盘语言开关:)
public ArrayList<Ride> parse (String str) throws JSONException 
{ 
    ArrayList<Ride> rides = new ArrayList<Ride>(); 


    try{ 
        JSONArray ridesJsonArray = new JSONArray(str); 

        Ride ride; 
        for (int i=0;i<ridesJsonArray.length();i++) 
        { 
            ride = new Ride(); 
            JSONObject o = ridesJsonArray.optJSONObject(i); 
            ride.rideId=o.optInt("id"); 
            ride.fullName=o.optString("fullname"); 
            ride.origin=o.optString("origin");               
            ride.destination=o.optString("destination"); 
            ride.date=o.optString("date"); 
            ride.time=o.optString("time"); 
            ride.phone=o.optString("phone"); 
            ride.email=o.optString("email"); 
            ride.comments=o.optString("comments"); 

            //log message to make sure the rides are coming from the server 
            Log.i("Rides", ride.fullName + " | "); 

            rides.add(ride);             
        } 
        return rides; 
        }                                                
    catch (Exception e) { 
        e.printStackTrace(); 
        return null; 
    } 
}