1

I want to make JSON object from database but there is one complex structure. I am not clear that how to make complex structure like "values" in following Json string.Help anybody,thanks in advance.This is the Json string which i want to make:

[{"type":"person1","id":null,"values":[[[32.3619,50.9291],[32.3604,50.9644],[32.3446,50.9395]]]}]

code I have tried yet.

                Session sess1 = sf.openSession();
                Query q = sess1.createQuery("from person");
                List l = q.list();
                Iterator itr = l.iterator();
                JSONArray jArray = new JSONArray();
                JSONObject jObj = new JSONObject();
                String id = null, lat, lng;
                while (itr.hasNext()) {
                    Person pobj = (Person) itr.next();
                    id = pobj.getId().toString();
                    lat = pobj.getLatitude();
                    lng = pobj.getLongitude();
                }
                jObj.put("type", "Person1");
                jObj.put("id", id);

                JSONArray jrray = new JSONArray();
                jArray.put(jObj);
                JSONObject jObjDevice = new JSONObject();
                jObjDevice.put("", jArray);
                System.out.println("json object created" + jObjDevice.toString());

4 Answers 4

1

Using jackson...

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class MapToJsonExample {
    private static final String pathToJsonResult = "example.json";

    public static void main(String[] args) {
        ObjectMapper objectMapper = new ObjectMapper();

        Map<String, Object> mapObject = new HashMap<String, Object>();

        mapObject.put("type", "person1");
        mapObject.put("id", null);

        List<Object> myList = new ArrayList<>();
        List<Double> point1 = Arrays.asList( 32.3619,50.9291 );
        List<Double> point2 = Arrays.asList( 32.3446,50.9395 );
        List<List<Double>> innerList = new ArrayList<>();
        List<List<List<Double>>> outerList = new ArrayList<>();
        innerList.add( point1 );
        innerList.add( point2 );
        outerList.add( innerList );
        mapObject.put( "values", outerList );

        try {
            objectMapper.writeValue(new File(pathToJsonResult), mapObject);
        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Here, using Windows and Java7,
Compile it..

javac -cp .;jackson-all-1.9.11.jar MapToJsonExample.java

Run it...

java -cp .;jackson-all-1.9.11.jar MapToJsonExample

Voila!

type example.json
{"values":[[[32.3619,50.9291],[32.3446,50.9395]]],"id":null,"type":"person1"}
Sign up to request clarification or add additional context in comments.

3 Comments

then can u give me some useful links or help in which structure like "values":[[[32.3619,50.9291],[32.3604,50.9644],[32.3446,50.9395]]]}] is build. I am new to json and i have to done it very quickly :(
I'd probably have to experiment some, which I think you might also try to do yourself. Another approach is illustrated here examples.javacodegeeks.com/core-java/json/jackson/…
Updated with example using Jackson. That declaration of outerList is pretty hideous, IMO, but it should fit the bill.
0

I would encourage you avoid all this boilerplate code and use JPA and frameworks like Jackson or similar. Business application developers should not really waste time on these infrastructure things :)))

Comments

0

I suppose you are using relational database like MySQL. To retrieve your record in Object form, you can leverage Hibernate ORM. You can find tons of tutorials on internet.

Comments

0

if you use java then make one modal class the same as database table field and result set of db convert to that modal class....modal class is java entity.....

base on that java entity use gson [com.google.code.gson] lib and convert it ...

you just googling lots of examples there.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.