2

I have a json array in database. I want to get only value ignoring the key and send it to ajax call.

Json string I have while saving:

{
    "cells": 
    [
        {
            "type": "devs.TooledModel",
            "position": 
            {
                "x": 200,
                "y": 100
            },
            "size": 
            {
                "width": 71,
                "height": 100
            },
                ".": 
            {
                    "magnet": false
            }
        }
    ]
}

I want to return the exact json array from database but I am getting a key appended because I am using a map in java servlet to retrieve json:

List<Map<String, Object>> result = new ArrayList<>();

    while (rSet.next()) {
        Map<String, Object> row = new HashMap<>();
        row.put("JSON_Diagram", rSet.getString("JSON_INFO"));
        result.add(row);
    }

json received: JSON_Diagram: "cells%5B0%5D%5Btype%5D=devs.TooledModel&cells..

How do I remove the key JSON_Diagram and get only value ? Tried with Object value = result.get(0); but didn't work

kittu
  • 6,662
  • 21
  • 91
  • 185

2 Answers2

3

You need to get a list of all the keys, loop over them and add them to your map and if you need only value add values only to list as shown in the example below:

Map<String,Object> row  = new HashMap<String,Object>();
    Iterator iter = rSet.keys();
    while(iter.hasNext()){
        String key = (String)iter.next();
        String value = rSet.getString(key);
        row.put(key,value);
        result.add(value);

    }
Mihir
  • 572
  • 1
  • 6
  • 24
  • Is this how json looks like when retrieved from db: "cells%5B0%5D%5Btype%5D=devs.TooledModel&cells Special characters like percent are getting added. Is this normal – kittu Aug 12 '15 at 06:11
2

So once you have the map just do a a map.values.

[http://docs.oracle.com/javase/7/docs/api/java/util/Map.html#values()][1]

Then just use the resultant collection!

Som Bhattacharyya
  • 3,972
  • 35
  • 54