-5
{ 
"to": {
        "names": [
          "Mubshar Pribno"
        ],
        "callerIds": [
          "92 336 4440247"
        ],
        "captions": [
          "Mubshar Pribno"
        ]
      }
}

please suggest me ho to create this type of Json format in java code please help me

3
  • 1
    Have you done any research into parsing JSON? If so, how far have you got so far? Do you have code that doesn't work? If so, post it in the question and explain what goes wrong. Commented Jul 31, 2015 at 5:48
  • I assume you want to parse it, then I would suggest searching for "JSON Java", since there are many, many possibilities for that out there. Until you decide on one and try some code, it's unfortunately hard to help you... Commented Jul 31, 2015 at 5:50
  • I want "names" array element wothout Key only value Commented Jul 31, 2015 at 5:51

4 Answers 4

0

For above json you must have this classes below:

Example.java

package com.example;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;

public class Example {

@Expose
private To to;
public To getTo() {
return to;
}
public void setTo(To to) {
this.to = to;
}
}

To.java

package com.example;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;

public class To {

@Expose
private List<String> names = new ArrayList<String>();
@Expose
private List<String> callerIds = new ArrayList<String>();
@Expose
private List<String> captions = new ArrayList<String>();

public List<String> getNames() {
return names;
}
public void setNames(List<String> names) {
this.names = names;
}
public List<String> getCallerIds() {
return callerIds;
}
public void setCallerIds(List<String> callerIds) {
this.callerIds = callerIds;
}
public List<String> getCaptions() {
return captions;
}
public void setCaptions(List<String> captions) {
this.captions = captions;
}

}

Then you convert this classes into json string as follows:

Gson gson = new Gson();
To myTo=new To();
myTo.setNames(...);
myTo.setCallerIds(...);
myTO.setCaptions(...);
Example exm=new Example();
exm.setTo(myTo);
System.out.println(gson.toJson(exm));
Sign up to request clarification or add additional context in comments.

Comments

0

GSON is a Java library from Google to convert Java objects to JSON. You can simply pass a Java object to the library function and it will return a JSON string.

Download: http://code.google.com/p/google-gson/

Example: http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/

Comments

0

I suggest you used Jackson.

This is cool tutorial

Comments

0

Hai you can create a json in java code itself for that you need to import the jar in the java project json-simple-1.1.1.jar and you can refer the link to know how to develop a json in java Click here to know how to create a json in java

2 Comments

thanks I solved myself
Thats cool if you find a solution for the question you can post the answer here to make it useful to others @ babar bashir

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.