JSONObject.put字符串方法将额外\字符添加到JSON字符串

时间:2014-05-29 09:39:38

标签: android json

我有一个json字符串,它有pid,name,price作为元素。我在logcat中显示它。但是这个字符串改变了,因为我使用put方法将json字符串添加到json对象。当我打印json对象' \' charcater被添加到每个键值对。我无法弄清楚究竟是什么导致了这个问题。

json是一个普通字符串,jObj是JSONObject。

我的logcat详细信息是:

json = {"products":[{"pid":"14","name":"zxc","price":"123456"},{"pid":"6","name":"Sony Xperia","price":"35000"},{"pid":"8","name":"Samsung Galaxy Note","price":"32000"},{"pid":"5","name":"htc","price":"26326"},{"pid":"9","name":"Nokia Lumia 800","price":"18000"},{"pid":"2","name":"iphone","price":"12345"},{"pid":"15","name":"sdjnas","price":"12243"},{"pid":"13","name":"Samsung S5222","price":"6500"},{"pid":"11","name":"Nokia C201","price":"4400"},{"pid":"7","name":"Nokia Asha 200","price":"4000"},{"pid":"1","name":"htc","price":"1234"},{"pid":"3","name":"htc","price":"1234"},{"pid":"4","name":"htc","price":"1234"},{"pid":"10","name":"aks","price":"1234"},{"pid":"12","name":"asd","price":"123"}],"success":1}

编写以下代码后:

jObj.put("details", json);

额外的角色' \'已添加。

json object = {"details":"{\"products\":[{\"pid\":\"14\",\"name\":\"zxc\",\"price\":\"123456\"},{\"pid\":\"6\",\"name\":\"Sony Xperia\",\"price\":\"35000\"},{\"pid\":\"8\",\"name\":\"Samsung Galaxy Note\",\"price\":\"32000\"},{\"pid\":\"5\",\"name\":\"htc\",\"price\":\"26326\"},{\"pid\":\"9\",\"name\":\"Nokia Lumia 800\",\"price\":\"18000\"},{\"pid\":\"2\",\"name\":\"iphone\",\"price\":\"12345\"},{\"pid\":\"15\",\"name\":\"sdjnas\",\"price\":\"12243\"},{\"pid\":\"13\",\"name\":\"Samsung S5222\",\"price\":\"6500\"},{\"pid\":\"11\",\"name\":\"Nokia C201\",\"price\":\"4400\"},{\"pid\":\"7\",\"name\":\"Nokia Asha 200\",\"price\":\"4000\"},{\"pid\":\"1\",\"name\":\"htc\",\"price\":\"1234\"},{\"pid\":\"3\",\"name\":\"htc\",\"price\":\"1234\"},{\"pid\":\"4\",\"name\":\"htc\",\"price\":\"1234\"},{\"pid\":\"10\",\"name\":\"aks\",\"price\":\"1234\"},{\"pid\":\"12\",\"name\":\"asd\",\"price\":\"123\"}],\"success\":1}\n"}

添加反斜杠是获取字符串的原因。 JSONException没有成功的值显示在logcat中:

JSONObject json = null;

List<NameValuePair> params = new ArrayList<NameValuePair>();
        JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);

        Log.d("All Products: ", json.toString());

        try
        {
            int success = json.getInt("success");

            if (success == 1)
            {
                products = json.getJSONArray(TAG_PRODUCTS);

                for (int i = 0; i < products.length(); i++)
                {
                    JSONObject c = products.getJSONObject(i);

                    String id = c.getString(TAG_PID);
                    String name = c.getString(TAG_NAME);

                    HashMap<String, String> map = new HashMap<String, String>();

                    map.put(TAG_PID, id);
                    map.put(TAG_NAME, name);

                    productsList.add(map);
                }
            }
            else
            {
                Intent i = new Intent(getApplicationContext(), NewProductActivity.class);
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
            }
        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }

logcat是:

05-29 15:54:21.772: W/ThrottleService(91): unable to find stats for iface rmnet0
05-29 15:54:26.192: I/ActivityManager(91): START {cmp=com.example.androidhive/.AllProductsActivity} from pid 588
05-29 15:54:26.192: W/WindowManager(91): Failure taking screenshot for (180x300) to layer 21010
05-29 15:54:26.502: I/WindowManager(91): createSurface Window{415a5d70 com.example.androidhive/com.example.androidhive.AllProductsActivity paused=false}: DRAW NOW PENDING
05-29 15:54:26.543: I/WindowManager(91): createSurface Window{415b8c88     com.example.androidhive/com.example.androidhive.AllProductsActivity paused=false}: DRAW NOW     PENDING
05-29 15:54:27.002: I/ARMAssembler(35): generated scanline__00000077:03010104_00008001_00000000 [ 89 ipp] (110 ins) at [0x41e00670:0x41e00828] in 1129891 ns
05-29 15:54:27.202: I/ActivityManager(91): Displayed com.example.androidhive/.AllProductsActivity: +980ms
05-29 15:54:27.472: D/dalvikvm(588): GC_CONCURRENT freed 125K, 3% free 10212K/10439K, paused 5ms+4ms
05-29 15:54:27.853: I/JSONPArser.java(588): json = {"products":    [{"pid":"14","name":"zxc","price":"123456"},{"pid":"6","name":"Sony Xperia","price":"35000"},{"pid":"8","name":"Samsung Galaxy Note","price":"32000"},{"pid":"5","name":"htc","price":"26326"},{"pid":"9","name":"Nokia Lumia 800","price":"18000"},{"pid":"2","name":"iphone","price":"12345"},{"pid":"15","name":"sdjnas","price":"12243"},{"pid":"13","name":"Samsung S5222","price":"6500"},{"pid":"11","name":"Nokia C201","price":"4400"},{"pid":"7","name":"Nokia Asha 200","price":"4000"},{"pid":"1","name":"htc","price":"1234"},{"pid":"3","name":"htc","price":"1234"},{"pid":"4","name":"htc","price":"1234"},{"pid":"10","name":"aks","price":"1234"},{"pid":"12","name":"asd","price":"123"}],"success":1}
05-29 15:54:27.892: I/JSONPArser.java(588): json object = {"details":"{\"products\":[{\"pid\":\"14\",\"name\":\"zxc\",\"price\":\"123456\"},{\"pid\":\"6\",\"name\":\"Sony Xperia\",\"price\":\"35000\"},{\"pid\":\"8\",\"name\":\"Samsung Galaxy Note\",\"price\":\"32000\"},{\"pid\":\"5\",\"name\":\"htc\",\"price\":\"26326\"},{\"pid\":\"9\",\"name\":\"Nokia Lumia 800\",\"price\":\"18000\"},{\"pid\":\"2\",\"name\":\"iphone\",\"price\":\"12345\"},{\"pid\":\"15\",\"name\":\"sdjnas\",\"price\":\"12243\"},{\"pid\":\"13\",\"name\":\"Samsung S5222\",\"price\":\"6500\"},{\"pid\":\"11\",\"name\":\"Nokia C201\",\"price\":\"4400\"},{\"pid\":\"7\",\"name\":\"Nokia Asha 200\",\"price\":\"4000\"},{\"pid\":\"1\",\"name\":\"htc\",\"price\":\"1234\"},{\"pid\":\"3\",\"name\":\"htc\",\"price\":\"1234\"},{\"pid\":\"4\",\"name\":\"htc\",\"price\":\"1234\"},{\"pid\":\"10\",\"name\":\"aks\",\"price\":\"1234\"},{\"pid\":\"12\",\"name\":\"asd\",\"price\":\"123\"}],\"success\":1}\n"}
05-29 15:54:27.892: I/JSONPArser.java(588): Line 27
05-29 15:54:27.902: I/JSONPArser.java(588): Line 30
05-29 15:54:27.902: D/All Products:(588): {"details":"{\"products\":[{\"pid\":\"14\",\"name\":\"zxc\",\"price\":\"123456\"},{\"pid\":\"6\",\"name\":\"Sony Xperia\",\"price\":\"35000\"},{\"pid\":\"8\",\"name\":\"Samsung Galaxy Note\",\"price\":\"32000\"},{\"pid\":\"5\",\"name\":\"htc\",\"price\":\"26326\"},{\"pid\":\"9\",\"name\":\"Nokia Lumia 800\",\"price\":\"18000\"},{\"pid\":\"2\",\"name\":\"iphone\",\"price\":\"12345\"},{\"pid\":\"15\",\"name\":\"sdjnas\",\"price\":\"12243\"},{\"pid\":\"13\",\"name\":\"Samsung S5222\",\"price\":\"6500\"},{\"pid\":\"11\",\"name\":\"Nokia C201\",\"price\":\"4400\"},{\"pid\":\"7\",\"name\":\"Nokia Asha 200\",\"price\":\"4000\"},{\"pid\":\"1\",\"name\":\"htc\",\"price\":\"1234\"},{\"pid\":\"3\",\"name\":\"htc\",\"price\":\"1234\"},{\"pid\":\"4\",\"name\":\"htc\",\"price\":\"1234\"},{\"pid\":\"10\",\"name\":\"aks\",\"price\":\"1234\"},{\"pid\":\"12\",\"name\":\"asd\",\"price\":\"123\"}],\"success\":1}\n"}
05-29 15:54:27.933: W/System.err(588): org.json.JSONException: No value for success
05-29 15:54:27.933: W/System.err(588):  at org.json.JSONObject.get(JSONObject.java:354)
05-29 15:54:27.933: W/System.err(588):  at org.json.JSONObject.getInt(JSONObject.java:443)
05-29 15:54:27.942: W/System.err(588):  at com.example.androidhive.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:110)
05-29 15:54:27.942: W/System.err(588):  at com.example.androidhive.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:1)
05-29 15:54:27.973: W/System.err(588):  at android.os.AsyncTask$2.call(AsyncTask.java:264)
05-29 15:54:27.973: W/System.err(588):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-29 15:54:27.973: W/System.err(588):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-29 15:54:27.973: W/System.err(588):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
05-29 15:54:27.973: W/System.err(588):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-29 15:54:27.982: W/System.err(588):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-29 15:54:27.982: W/System.err(588):  at java.lang.Thread.run(Thread.java:856)

2 个答案:

答案 0 :(得分:5)

JSONObject jsonObj = new JSONObject(json);

jsonObj.get("products");

JSONObject newJsonObj = new JSONObject();
newJsonObj.put("whatever", jsonObj.get("products"));

如果你想保持json而不转义,你需要将jsonObject添加到JsonArray。

JSONArray jsonArray = new JSONArray();
jsonArray.put(jsonObj);

稍后用

取回
for (int i = 0; i < jsonArray.length(); i++)  { 
    JSONObject jsonObj = jsonArray.getJSONObject(i); 
 // then you can start filtering out the array.
    if (jsonObj.has("products")) {
         JSONArray nJsonArray = new JSONArray(jsonObj.get("products"));
          for (......) {} 
}

答案 1 :(得分:0)

这是一个功能强大的JSON对象。我用Cocoa JSON编辑器创建了一个,没有错误。我不知道它添加了什么反斜杠,但我可以告诉你它是好的