2014-10-12 57 views
0

我製作了一個解析來自URL的數據的android應用程序。只要json數據returnedL中沒有引號,應用程序就可以正常工作。有沒有一種方法來解析與Java JSON數據,當數據有這樣的標誌:用引號解析json數據

{ 
"cate": 
[ 
    { 
    "title": "this is the "title" like ", 
    "EdId": "445", 
    "date": "Sep 25, 2014" 
    } 
] 
} 

這裏是代碼我一起工作:

public class Guys extends Activity { 
    ListView list; 
    TextView title; 
    TextView EdId; 
    TextView date; 
    Button Btngetdata; 
    //Button nextac; 
    ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>(); 

    //URL to get JSON Array 
    private static String url = "http://10.10.10.2/test/ff.php"; 



    //JSON Node Names 
    //private static final String ca 
    private static final String TAG_CAT = "cate"; 
    private static final String TAG_TITLE = "title"; 
    private static final String TAG_EDID = "EdId"; 
    private static final String TAG_DATE = "date"; 

    JSONArray cate = null; 




    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.test_main); 
     oslist = new ArrayList<HashMap<String, String>>(); 

     new JSONParse().execute(); 



    } 



    private class JSONParse extends AsyncTask<String, String, JSONObject> { 
     private ProgressDialog pDialog; 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      title = (TextView)findViewById(R.id.title); 
      EdId = (TextView)findViewById(R.id.edid); 
      date = (TextView)findViewById(R.id.date); 
      pDialog = new ProgressDialog(Guys.this); 
      pDialog.setMessage("Getting Data ..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 



     } 

     @Override 
     protected JSONObject doInBackground(String... args) { 
      String jn="ff'f'd"; 
      JSONParser jParser = new JSONParser(); 

      // Getting JSON from URL 
      JSONObject json = jParser.getJSONFromUrl(url); 
        json.toString();  


      return json; 
     } 
     @Override 
     protected void onPostExecute(JSONObject json) { 
      pDialog.dismiss(); 
      try { 



       //json = json.substring(7); 

        // Getting JSON Array from URL 
        cate = json.getJSONArray(TAG_CAT); 
        for(int i = 0; i < cate.length(); i++){ 
        JSONObject c = cate.getJSONObject(i); 

        // Storing JSON item in a Variable 
        String title = c.getString(TAG_TITLE); 
        String EdId = c.getString(TAG_EDID); 
        //String date = c.getString(TAG_API); 




        // Adding value HashMap key => value 


        HashMap<String, String> map = new HashMap<String, String>(); 

        map.put(TAG_TITLE, title); 
        map.put(TAG_EDID, EdId); 
        //map.put(TAG_API, date); 

        oslist.add(map); 
        list=(ListView)findViewById(R.id.list); 





        ListAdapter adapter = new SimpleAdapter(Guys.this, oslist, 
          R.layout.list_v, 
          new String[] { TAG_TITLE}, new int[] { 
            R.id.title}); 

        list.setAdapter(adapter); 
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

         @Override 
         public void onItemClick(AdapterView<?> parent, View view, 
               int position, long id) { 
          Toast.makeText(Guys.this, "You Clicked at "+oslist.get(+position).get("EdId"), Toast.LENGTH_SHORT).show(); 
          Intent myIntent = new Intent(Guys.this, SimpleBrowser.class); 


          String idurl = oslist.get(+position).get("EdId"); 
          String idsend = idurl; 

          myIntent.putExtra("id",idurl.toString()); 
          Guys.this.startActivity(myIntent); 
         } 
        }); 

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




      } 


     } 
    } 





} 
+1

您的JSON是無效的! – mmlooloo 2014-10-12 13:33:16

+0

我很想知道爲什麼這個問題的編輯被批准了,特別是因爲它引入了新的拼寫錯誤,並且通過刪除「引號」中的「引用」一詞來掩蓋了問題。 – 2014-10-12 15:39:07

+0

我不能從json對象中刪除引號,因爲這個json對象是聯機的,我嘗試創建一個android應用程序來解析來自這個網站的新聞 所以如果有一些消息有引用我的json沒有解析數據 – 2014-10-12 15:46:41

回答

1

我認爲這個問題是與系統生產json字符串,在生產者系統中,您需要轉義"字符替換爲\"或使用' charachter替代。

0

您可以使用gson類來解析json響應。 您需要創建一個類

class Data{ 

    ArrayList<Data> cate; 
    getCateList(){ 
     return cate; 
    } 
    String title; 
    String EdId; 
    String date; 
} 


InputStream source = retrieveStream(url); 
Gson gson = new Gson(); 
Reader reader = new InputStreamReader(source); 
Data response = gson.fromJson(reader, Data.class); 

閱讀下面的教程:http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html