简体   繁体   中英

parsing json data with quotation

I have produced an android app for parsing data from a URL. The application works fine as long as there are no quotation marks in the json data returnedL. Is there a way to parse json data with Java, when data has marks like this:

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

Here is the code I am working with:

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();




            }


         }
    }





}

我认为问题出在生产json字符串的系统上,在生产者系统中,您需要转义"\\"替换它的字符\\"或使用' charachter来代替。

You can use gson class to parse json response. You need to create one class

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);

Read following tutorial : http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM