0

I am developing an Android app in which I have to send LinkedHashMap results by API but the problem what I am getting is format of result is different. How can I put keys and values both in inverted commas?

I'm getting result like this:

list: {0=816444014066, 1=747083010945, 2=816444010969}

And I want result like this:

list: {"0" : "816444014066","1" : "747083010945","2" : "816444010969"}

How to change the format of result?

halfer
  • 19,824
  • 17
  • 99
  • 186
Abhishek kumar
  • 4,347
  • 8
  • 29
  • 44

4 Answers4

2

Use My Answer. It worked for me.

LinkedHashMap<String, String> data = new LinkedHashMap<String, String>();
// Instantiate a new Gson instance.
        Gson gson = new Gson();

        // Convert the ordered map into an ordered string.
        String json = gson.toJson(data, LinkedHashMap.class);

        // Print ordered string.
        Log.e("list", ""+json); //  {"0" : "816444014066","1" : "747083010945","2" : "816444010969"}
Shivam Tripathi
  • 1,405
  • 3
  • 19
  • 37
1

To get the quotes you need to make your keys and values String in your LinkedHashMap

Edit:

maybe what you need is already provided in this answer

Peppermint Paddy
  • 1,269
  • 9
  • 14
1

In Java you can put quotes to String with :

String value = " \"1\" ";

You could do it like this:

Map<String, String> linkedmap = new LinkedHashMap<>();
linkedHashMap.put(setQuotes("1"), setQuotes("5445454"));

public static String setQuotes(String value){
     String result = "";
     if(!value.isEmpty()){
          result = "\"" + value + "\"";   
     }
     return result;
}

If you print it in the console, it returns:

{"1"="5445454"}
M. Wojcik
  • 2,301
  • 3
  • 23
  • 31
0

I think that possibility is to create your own Map class that extends LinkedHashMap and to create and implement in it method with behavior similar to behavior of toString() method. This link might help you to get started with implementation of that method:

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/util/AbstractMap.java#AbstractMap.toString%28%29

Przemysław Moskal
  • 3,551
  • 2
  • 12
  • 21