2014-04-15 132 views
0

我現在有一個問題,解析JSON數據。 有幹啥錯我的JSON數據或怎麼回事, 我找不到我的錯誤。 ALways如果我運行我的應用程序這一個也崩潰,因爲我的JSON是空的。JSON數據解析問題

我的JSON:

{ 
"android":[ 
{ 
"ver": "1.5", 
"name": "Cupcake", 
"api": "API level 3" 
}, 
{ 
"ver": "1.6", 
"name": "Donut", 
"api": "API level 4" 
}, 
{ 
"ver": "2.0-2.1", 
"name": "Eclair", 
"api": "API level 5-7" 
}, 
{ 
"ver": "2.2", 
"name": "Froyo", 
"api": "API level 8" 
}, 
{ 
"ver": "2.3", 
"name": "Gingerbread", 
"api": "API level 9-10" 
}, 
{ 
"ver": "3.0-3.2", 
"name": "Honeycomb", 
"api": "API level 11-13" 
}, 
{ 
"ver": "4.0", 
"name": "Ice Cream Sandwich", 
"api": "API level 14-15" 
}, 
{ 
"ver": "4.1-4.3", 
"name": "JellyBean", 
"api": "API level 16-18" 
}, 
{ 
"ver": "4.4", 
"name": "KitKat", 
"api": "API level 19" 
} 
] 
} 

從logcat的輸出:

Error parsing data org.json.JSONException: Unterminated array at character 15 of {n"android":[n{n"ver": "1.5",n"name": "Cupcake",n"api": "API level 3"n},n{n"ver": "1.6",n"name": "Donut",n"api": "API level 4"n},n{n"ver": "2.0-2.1",n"name": "Eclair",n"api": "API level 5-7"n},n{n"ver": "2.2",n"name": "Froyo",n"api": "API level 8"n},n{n"ver": "2.3",n"name": "Gingerbread",n"api": "API level 9-10"n},n{n"ver": "3.0-3.2",n"name": "Honeycomb",n"api": "API level 11-13"n},n{n"ver": "4.0",n"name": "Ice Cream Sandwich",n"api": "API level 14-15"n},n{n"ver": "4.1-4.3",n"name": "JellyBean",n"api": "API level 16-18"n},n{n"ver": "4.4",n"name": "KitKat",n"api": "API level 19"n}n]n}n 

MainAct:

import java.util.ArrayList; 
import java.util.HashMap; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import android.app.Activity; 
import android.app.ProgressDialog; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.Button; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity { 
    ListView list; 
    TextView ver; 
    TextView name; 
    TextView api; 
    Button Btngetdata; 
    ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>(); 
    //URL to get JSON Array 
    private static String url = "*****"; 
    //JSON Node Names 
    private static final String TAG_OS = "android"; 
    private static final String TAG_VER = "ver"; 
    private static final String TAG_NAME = "name"; 
    private static final String TAG_API = "api"; 
    JSONArray android = null; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     oslist = new ArrayList<HashMap<String, String>>(); 
     Btngetdata = (Button)findViewById(R.id.getdata); 
     Btngetdata.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       new JSONParse().execute(); 
      } 
     }); 
    } 
    private class JSONParse extends AsyncTask<String, String, JSONObject> { 
     private ProgressDialog pDialog; 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      ver = (TextView)findViewById(R.id.vers); 
      name = (TextView)findViewById(R.id.name); 
      api = (TextView)findViewById(R.id.api); 
      pDialog = new ProgressDialog(MainActivity.this); 
      pDialog.setMessage("Getting Data ..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
     } 
     @Override 
     protected JSONObject doInBackground(String... args) { 
      JSONParser jParser = new JSONParser(); 
      // Getting JSON from URL 
      JSONObject json = jParser.getJSONFromUrl(url); 
      return json; 
     } 
     @Override 
     protected void onPostExecute(JSONObject json) { 
      pDialog.dismiss(); 
      try { 
       // Getting JSON Array from URL 
       android = json.getJSONArray(TAG_OS); 
       for(int i = 0; i < android.length(); i++){ 
        JSONObject c = android.getJSONObject(i); 
        // Storing JSON item in a Variable 
        String ver = c.getString(TAG_VER); 
        String name = c.getString(TAG_NAME); 
        String api = c.getString(TAG_API); 
        // Adding value HashMap key => value 
        HashMap<String, String> map = new HashMap<String, String>(); 
        map.put(TAG_VER, ver); 
        map.put(TAG_NAME, name); 
        map.put(TAG_API, api); 
        oslist.add(map); 
        list=(ListView)findViewById(R.id.list); 
        ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist, 
          R.layout.list_v, 
          new String[] { TAG_VER,TAG_NAME, TAG_API }, new int[] { 
          R.id.vers,R.id.name, R.id.api}); 
        list.setAdapter(adapter); 
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
         @Override 
         public void onItemClick(AdapterView<?> parent, View view, 
               int position, long id) { 
          Toast.makeText(MainActivity.this, "You Clicked at "+oslist.get(+position).get("name"), Toast.LENGTH_SHORT).show(); 
         } 
        }); 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

JSONParser:

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.UnsupportedEncodingException; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONException; 
import org.json.JSONObject; 
import android.util.Log; 
public class JSONParser { 
    static InputStream is = null; 
    static JSONObject jObj = null; 
    static String json = ""; 
    // constructor 
    public JSONParser() { 
    } 
    public JSONObject getJSONFromUrl(String url) { 
     // Making HTTP request 
     try { 
      // defaultHttpClient 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 
      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent(); 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "iso-8859-1"), 8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "n"); 
      } 
      is.close(); 
      json = sb.toString(); 
     } catch (Exception e) { 
      Log.e("Buffer Error", "Error converting result " + e.toString()); 
     } 
     // try parse the string to a JSON object 
     try { 
      jObj = new JSONObject(json); 
     } catch (JSONException e) { 
      Log.e("JSON Parser", "Error parsing data " + e.toString()); 
     } 
     // return JSON String 
     return jObj; 
    } 
} 
+0

檢出對我有效,看起來像你上面列出的例子中沒有出現錯誤輸出中的額外'n'。 – draksia

+0

請分享您的代碼 – meda

+0

剛剛編輯 – user3530080

回答

0

JSON解析器需要一個字符串。如果您提供了您發佈的確切文本,則由java將其解析爲未知對象的數組。嘗試將您的文本用引號括起來並轉義新行。恩。

String JsonString = "{\n\"android\":[\n{\n\"ver\": \"1.5\",\n\"name\": \"Cupcake\",\n\"api\": \"API level 3\"\n}]}"; 

當然這使得這不是你實際擁有的假設;無條件的,你應該總是張貼源代碼,以便它是明確的。

0

使用喜定的函數解析,只要傳遞完整的字符串函數,它是經過全面測試工作的代碼!

public void parseJson(String s) throws JSONException { 

     JSONObject object = new JSONObject(s); 
     JSONArray jarray = object.getJSONArray("android"); 
     for (int i = 0; i < jarray.length(); i++) { 
      JSONObject jobject = jarray.getJSONObject(i); 
      String ver = jobject.getString("ver"); 
      Log.d("ver=", ver); 
      String name = jobject.getString("name"); 
      Log.d("name=", name); 
      String api = jobject.getString("api"); 
      Log.d("api=", api); 
     } 
    }