conversion from string to json object android

后端 未结 8 1743
误落风尘
误落风尘 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条回答
  • try this:

    String json = "{'phonetype':'N95','cat':'WP'}";
    
    0 讨论(0)
  • 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());
        }
    
    0 讨论(0)
提交回复
热议问题