-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

Babar Bashir
  • 193
  • 3
  • 12
  • 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. – Jon Skeet Jul 31 '15 at 05: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... – Florian Schaetz Jul 31 '15 at 05:50
  • I want "names" array element wothout Key only value – Babar Bashir Jul 31 '15 at 05:51

4 Answers4

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/

Adarsh Yadav
  • 3,752
  • 3
  • 24
  • 46
0

I suggest you used Jackson.

This is cool tutorial

Mohammad Hossein Gerami
  • 1,360
  • 1
  • 10
  • 26
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));
Jemshit
  • 9,501
  • 5
  • 69
  • 106
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

siva
  • 244
  • 2
  • 11