conversion from string to json object android

后端 未结 8 1787
误落风尘
误落风尘 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:18

    Here is the code, and you can decide which
    (synchronized)StringBuffer or faster StringBuilder to use.

    Benchmark shows StringBuilder is Faster.

    public class Main {
                int times = 777;
                long t;
    
                {
                    StringBuffer sb = new StringBuffer();
                    t = System.currentTimeMillis();
                    for (int i = times; i --> 0 ;) {
                        sb.append("");
                        getJSONFromStringBuffer(String stringJSON);
                    }
                    System.out.println(System.currentTimeMillis() - t);
                }
    
                {
                    StringBuilder sb = new StringBuilder();
                    t = System.currentTimeMillis();
                    for (int i = times; i --> 0 ;) {
                         getJSONFromStringBUilder(String stringJSON);
                        sb.append("");
                    }
                    System.out.println(System.currentTimeMillis() - t);
                }
                private String getJSONFromStringBUilder(String stringJSONArray) throws JSONException {
                    return new StringBuffer(
                           new JSONArray(stringJSONArray).getJSONObject(0).getString("phonetype"))
                               .append(" ")
                               .append(
                           new JSONArray(employeeID).getJSONObject(0).getString("cat"))
                          .toString();
                }
                private String getJSONFromStringBuffer(String stringJSONArray) throws JSONException {
                    return new StringBuffer(
                           new JSONArray(stringJSONArray).getJSONObject(0).getString("phonetype"))
                               .append(" ")
                               .append(
                           new JSONArray(employeeID).getJSONObject(0).getString("cat"))
                          .toString();
                }
            }
    
    0 讨论(0)
  • 2020-11-30 01:20

    Remove the slashes:

    String json = {"phonetype":"N95","cat":"WP"};
    
    try {
    
        JSONObject obj = new JSONObject(json);
    
        Log.d("My App", obj.toString());
    
    } catch (Throwable t) {
        Log.e("My App", "Could not parse malformed JSON: \"" + json + "\"");
    }
    
    0 讨论(0)
  • 2020-11-30 01:20

    May be below is better.

    JSONObject jsonObject=null;
        try {
            jsonObject=new JSONObject();
            jsonObject.put("phonetype","N95");
            jsonObject.put("cat","wp");
            String jsonStr=jsonObject.toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    
    0 讨论(0)
  • 2020-11-30 01:27

    just try this , finally this works for me :

    //delete backslashes ( \ ) :
                data = data.replaceAll("[\\\\]{1}[\"]{1}","\"");
    //delete first and last double quotation ( " ) :
                data = data.substring(data.indexOf("{"),data.lastIndexOf("}")+1);
                JSONObject json = new JSONObject(data);
    
    0 讨论(0)
  • 2020-11-30 01:29

    its work

        String json = "{\"phonetype\":\"N95\",\"cat\":\"WP\"}";
    
        try {
    
            JSONObject obj = new JSONObject(json);
    
            Log.d("My App", obj.toString());
            Log.d("phonetype value ", obj.getString("phonetype"));
    
        } catch (Throwable tx) {
            Log.e("My App", "Could not parse malformed JSON: \"" + json + "\"");
        }
    
    0 讨论(0)
  • 2020-11-30 01:32

    To get a JSONObject or JSONArray from a String I've created this class:

    public static class JSON {
    
         public Object obj = null;
         public boolean isJsonArray = false;
    
         JSON(Object obj, boolean isJsonArray){
             this.obj = obj;
             this.isJsonArray = isJsonArray;
         }
    }
    

    Here to get the JSON:

    public static JSON fromStringToJSON(String jsonString){
    
        boolean isJsonArray = false;
        Object obj = null;
    
        try {
            JSONArray jsonArray = new JSONArray(jsonString);
            Log.d("JSON", jsonArray.toString());
            obj = jsonArray;
            isJsonArray = true;
        }
        catch (Throwable t) {
            Log.e("JSON", "Malformed JSON: \"" + jsonString + "\"");
        }
    
        if (object == null) {
            try {
                JSONObject jsonObject = new JSONObject(jsonString);
                Log.d("JSON", jsonObject.toString());
                obj = jsonObject;
                isJsonArray = false;
            } catch (Throwable t) {
                Log.e("JSON", "Malformed JSON: \"" + jsonString + "\"");
            }
        }
    
        return new JSON(obj, isJsonArray);
    }
    

    Example:

    JSON json = fromStringToJSON("{\"message\":\"ciao\"}");
    if (json.obj != null) {
    
        // If the String is a JSON array
        if (json.isJsonArray) {
            JSONArray jsonArray = (JSONArray) json.obj;
        }
        // If it's a JSON object
        else {
            JSONObject jsonObject = (JSONObject) json.obj;
        }
    }
    
    0 讨论(0)
提交回复
热议问题