用引号解析json数据

时间:2014-10-12 12:26:22

标签: java android json parsing

我已经制作了一个用于解析URL数据的Android应用程序。只要在返回的json数据中没有引号,应用程序就可以正常工作。当数据有这样的标记时,有没有办法用Java解析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();




            }


         }
    }





}

2 个答案:

答案 0 :(得分:1)

我认为问题在于系统产生json字符串,在生产者系统中,您需要转义"字符,将其替换为\"或使用{{1}而是charachter。

答案 1 :(得分: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