Problem:
how to convert to JSON to MAP and Vice-versa in java?
Solution:
I used org.json library simple and best for json operations.
//import
import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;
import org.json.JSONTokener;
long start = System.currentTimeMillis();
String test = "12333";
Map<String,String> map = new HashMap<>();
for (int i = 0; i < 10; i++)
{
map.put("key"+i, i+test);
}
//System.out.println(map);
long pause1 = System.currentTimeMillis();
JSONObject json = new JSONObject(map);
json.toString(4);
long pause2 = System.currentTimeMillis();
System.out.println("Map 2 JSON---"+(pause2-pause1)+"Millis");
JSONObject object = (JSONObject) new JSONTokener(json.toString()).nextValue();
object.toString(4);
long pause3 = System.currentTimeMillis();
System.out.println("JSON to MAP---"+(pause3-pause2)+"Millis");
No comments:
Post a Comment
Please comment here