0

I am trying to put NameValuePairdata to JSONObject. The NameValuePairdata has name as String and Value as JSONArray. Now when I tried to put this NameValuePairdata in a JSONObject, the jsonobject converts the JSONArray value to strings.

Please check below code for more details:

constructing NameValuePair:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);

    nameValuePairs.add(new BasicNameValuePair("id", "1"));      

    JSONArray arr = new JSONArray();
    if(arrBean != null && arrBean.size() > 0) {
        for (Bean bean : arrBean) {
            JSONObject idsJsonObject = new JSONObject();
            try {
                idsJsonObject.put("min", bean.getMin());
                idsJsonObject.put("m1", bean.getMin());
                idsJsonObject.put("sec", bean.getSec());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            arr.put(idsJsonObject);
        }
    }
    nameValuePairs.add(new BasicNameValuePair("records", arr.toString()));

Constructing JSONObject to be send to HttpPost:

JSONObject JSONObjectData = new JSONObject();

    for (NameValuePair nameValuePair : nameValuePairs) {
        try {
            JSONObjectData.put(nameValuePair.getName(), nameValuePair.getValue());
        } catch (JSONException e) {

        }
    }

As shown above the JSONObjectData results into the following:

{"id":"1","records":"[{\"min\":\"610\",\"m1\":\"10\",\"sec\":\"\"},{\"min\":\"610\",\"m1\":\"10\",\"sec\":\"\"},{\"min\":\"610\",\"m1\":\"10\",\"sec\":\"\"},{\"min\":\"610\",\"m1\":\"10\",\"sec\":\"\"},{\"min\":\"610\",\"m1\":\"10\",\"sec\":\"\"},{\"min\":\"610\",\"m1\":\"10\",\"sec\":\"\"},{\"min\":\"610\",\"m1\":\"10\",\"sec\":\"\"},{\"min\":\"610\",\"m1\":\"10\",\"sec\":\"\"},{\"min\":\"610\",\"m1\":\"10\",\"sec\":\"\"},{\"min\":\"610\",\"m1\":\"10\",\"sec\":\"\"}]"}

You can see, it automatically appends \"value\" inside the array values. E.x. \"min\",\"m1\" etc...

Any body has any idea how to avoid appending these \"value\".

Please let me know. Thanks in advance.

user435062
  • 123
  • 2
  • 4
  • 9
  • This may help http://stackoverflow.com/questions/18192891/conversion-from-string-to-json-object-android – Pankaj Kumar Apr 14 '14 at 13:48
  • try this link http://stackoverflow.com/questions/12142238/add-jsonarray-to-jsonobject – RQube Apr 14 '14 at 13:50
  • Did you try to find out what nameValuePair.getName() and nameValuePair.getValue() return? Please share this information. Try to Log these values. – Parth Kapoor Apr 14 '14 at 14:27
  • nameValuePair.getName() and nameValuePair.getValue() return proper strings that I constructed initially. At the time of JSONObject.put(), the values get changed. – user435062 Apr 14 '14 at 14:48

1 Answers1

2

Both nameValuePair.getName() and nameValuePair.getValue() return strings, so they are added to your json as strings. You should pass a JSONArray object as a second parameter in JSONObjectData.put().

As BasicNameValuePair accept only string values, try to use HashMap<String, Object> instead or recreate the JSONArray from its String representation.

  • Hi Alexander, to recreate the JSONArray from its String representation, should i replace the **\"** with **"** or is there any other way to recreate. please comment. Thanks. – user435062 Apr 14 '14 at 15:16
  • The quotes are added when you serialize a JSONObject with String values into it. So `new JSONArray(nameValuePair.getValue())` should work. – Alexander Sukharev Apr 14 '14 at 16:30
  • Thanks Alexander. I tried `new JSONArray(nameValuePair.getValue())` and it's working fine. – user435062 Apr 15 '14 at 06:02