conversion from string to json object android

后端 未结 8 1869
误落风尘
误落风尘 2020-11-30 00:39

I am working on an Android application. In my app I have to convert a string to Json Object, then parse the values. I checked for a solution in stackoverflow and found simil

8条回答
  •  一生所求
    2020-11-30 01:43

    You just need the lines of code as below:

       try {
            String myjsonString = "{\"phonetype\":\"N95\",\"cat\":\"WP\"}";
            JSONObject jsonObject = new JSONObject(myjsonString );
            //displaying the JSONObject as a String
            Log.d("JSONObject = ", jsonObject.toString());
            //getting specific key values
            Log.d("phonetype = ", jsonObject.getString("phonetype"));
            Log.d("cat = ", jsonObject.getString("cat");
        }catch (Exception ex) {
             StringWriter stringWriter = new StringWriter();
             ex.printStackTrace(new PrintWriter(stringWriter));
             Log.e("exception ::: ", stringwriter.toString());
        }
    

提交回复
热议问题