-6

I have this string

"{id={date=1467991309000, time=1467991309000, timestamp=1467991309, timeSecond=1467991309, inc=-360249353, machine=-705844029, new=false}, id_lista={date=1467991309000, time=1467991309000, timestamp=1467991309, timeSecond=1467991309, inc=-360249354, machine=-705844029, new=false}, id_documento=1297183, estado=1, fecha_ing=1467991309026, fecha_mod=1468010645484, identificador_r=null, titulo=null, pais=null, sector=null, url=null, dato1=null, dato2=null}"

How can I Parsing in java to get something like this Map<String,Object>

    id:{}
    id_lista:{}
    id_documento:123
    estado:1
    fecha_ing:1467991309026
    etc..

Update:

  • Finally I cast to JSONArray to get the values.
Bosses
  • 29
  • 1
  • 8

2 Answers2

1

Do you really mean java.lang.Object or do you mean a class of your own making?

You can get into the java world if you have appropriately defined a your class with the following (Google Gson):

BossesClass hisClass = new Gson().fromJson(bossesString, BossesClass.class);

What you use as the key value (a String) in your map is your decision

Vladimir Vagaytsev
  • 2,871
  • 9
  • 33
  • 36
Daniel Schmidt
  • 401
  • 5
  • 5
0

It looks to me as if you have an almost JSON-format string. Depending on what you want to use your map for, maybe you want to look into using org.json.JSONObject instead? (This is especially nice when you have nested information the way you have in your example-string.)

To get a JSONObject from your string you first have to replace all the equal signs with colons.

String jsonString = "your string here".replace("=",":");

Then you can create a JSONObject.

JSONObject jsonObj = new JSONObject(jsonString);

If you anyway want to have the map, there is answers here and here about getting from JSONObject to Map.

    public void parse(String json)  {
       JsonFactory factory = new JsonFactory();

       ObjectMapper mapper = new ObjectMapper(factory);
       JsonNode rootNode = mapper.readTree(json);  

       Iterator<Map.Entry<String,JsonNode>> fieldsIterator = rootNode.fields();
       while (fieldsIterator.hasNext()) {

           Map.Entry<String,JsonNode> field = fieldsIterator.next();
           System.out.println("Key: " + field.getKey() + "\tValue:" + field.getValue());
       }
}
Community
  • 1
  • 1
cissi
  • 13
  • 6