0

I want to send this serialize Json in Soap web service parameter to get data from server. I don know how to serialize Json like below.

I am doing this -

JSONObject jsonObject = new JSONObject();
            try {

                JSONObject obj = new JSONObject();
                //obj.put("MeterSrNo", txtMeterSrMo.getText().toString());
                obj.put("MeterSrNo", txtMeterSrMo.getText().toString());
                jsonObject.put("obj", obj);
                jsonObject.put("SPName", "XXMFU_GETMobilityDetail");
            } catch (JSONException e) {
                e.printStackTrace();
            }

And its giving me below output-

{'obj':{"MeterSrNo":"5"},'SPName':'XXMFU_GETMobilityDetail'}

But i want below output. How can i achieve this?

"{'\obj\':{\"MeterSrNo\":\"5\"},'\SPName\':'XXMFU_GETMobilityDetail'}"
Soham
  • 4,397
  • 11
  • 43
  • 71
  • Have you googled it?There's a ton of examples out there http://blog.brianbuikema.com/2010/04/android-how-to-deserialize-both-xml-and-json/ – Soham Jul 13 '16 at 08:02
  • 1
    This question could be resolved by 3 seconds with trivial googling of the subject, has no value at all. – Farside Jul 13 '16 at 08:25
  • Possible duplicate of [JSON on Android - serialization](http://stackoverflow.com/questions/7346786/json-on-android-serialization) – Soham Jul 13 '16 at 08:45

2 Answers2

0

By using Gson Library you can do serialization and desertification.

Serialization

new Gson().toJson(object)

Deserialization

new Gson().fromJson(jsonString, MyClass.class);

Android Surya
  • 544
  • 3
  • 17
  • Thanks Surya. Now i am getting below error- {serializeNulls:falsefactories:[Factory[type=com.google.gson.JsonElement,adapter=com.google.gson.internal.bind.TypeAdapters$25@15bdd364], com.google.gson.internal.bind.ObjectTypeAdapter$1@3b93a9cd, Factory[type=java.lang.String,adapter=com.google.gson.internal.bind.TypeAdapters$13@3d29f282], com.google.gson.internal.bind.DateTypeAdapter$1@12f1ed7e, com.google.gson.internal.Excluder@23f42e56, com.google.gson.internal.bind.CollectionTypeAdapterFactory@219a1ed7, –  Jul 13 '16 at 08:40
  • @MoinKhan r u using Date object ? same problem you can find the solution http://stackoverflow.com/questions/15114876/android-converting-an-object-to-json-format-with-gson-not-working – Android Surya Jul 13 '16 at 08:53
  • Still its giving same error. I am giving my code below- JSONObject jsonObject = new JSONObject(); try { JSONObject obj = new JSONObject(); //obj.put("MeterSrNo", txtMeterSrMo.getText().toString()); obj.put("MeterSrNo", 5); jsonObject.put("obj", obj); jsonObject.put("SPName", "XXMFU_GETMobilityDetail"); } catch (JSONException e) { e.printStackTrace(); } Gson gson = new GsonBuilder().setDateFormat(DateFormat.FULL, DateFormat.FULL).create(); request.addProperty(parameter_getlistdata, (my).toString()); –  Jul 13 '16 at 09:45
0

Try following way,

  JSONObject jsonObject = new JSONObject();
  JSONObject jsonMeterObject = new JSONObject();

    try {
        jsonMeterObject.put("MeterSrNo","5");
        jsonObject.put("obj",jsonMeterObject);
        jsonObject.put("SPName","XXMFU_GETMobilityDetail");
    } catch (JSONException e) {
        e.printStackTrace();
    }

Output

{
"obj":{
"MeterSrNo":"5"
},
"SPName":"XXMFU_GETMobilityDetail"
}
Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
Arjun saini
  • 4,223
  • 3
  • 23
  • 51