Java、java数据库list map只有三列id和name、mapname怎么样从java数据库list map读出来调整成这种格式的json数据啊

JDBCType (Java Platform SE 8 )
JavaScript is disabled on your browser.
Java&&PlatformStandard&Ed.&8
Enum JDBCType
java.sql.JDBCType
All Implemented Interfaces:
public enum JDBCType
extends &&
implements
Defines the constants that are used to identify generic
SQL types, called JDBC types.
Enum Constant Summary
Enum Constants&
Enum Constant and Description
Identifies the generic SQL type ARRAY.
Identifies the generic SQL type BIGINT.
Identifies the generic SQL type BINARY.
Identifies the generic SQL type BIT.
Identifies the generic SQL type BLOB.
Identifies the generic SQL type BOOLEAN.
Identifies the generic SQL type CHAR.
Identifies the generic SQL type CLOB.
Identifies the generic SQL type DATALINK.
Identifies the generic SQL type DATE.
Identifies the generic SQL type DECIMAL.
Identifies the generic SQL type DISTINCT.
Identifies the generic SQL type DOUBLE.
Identifies the generic SQL type FLOAT.
Identifies the generic SQL type INTEGER.
Indicates that the SQL type
is database-specific and gets mapped to a Java object that can be
accessed via the methods getObject and setObject.
Identifies the generic SQL type LONGNVARCHAR.
Identifies the generic SQL type LONGVARBINARY.
Identifies the generic SQL type LONGVARCHAR.
Identifies the generic SQL type NCHAR.
Identifies the generic SQL type NCLOB.
Identifies the generic SQL value NULL.
Identifies the generic SQL type NUMERIC.
Identifies the generic SQL type NVARCHAR.
Indicates that the SQL type
is database-specific and gets mapped to a Java object that can be
accessed via the methods getObject and setObject.
Identifies the generic SQL type REAL.
Identifies the generic SQL type REF.
Identifies the generic SQL type REF_CURSOR.
Identifies the SQL type ROWID.
Identifies the generic SQL type SMALLINT.
Identifies the generic SQL type SQLXML.
Identifies the generic SQL type STRUCT.
Identifies the generic SQL type TIME.
Identifies the generic SQL type TIME_WITH_TIMEZONE.
Identifies the generic SQL type TIMESTAMP.
Identifies the generic SQL type TIMESTAMP_WITH_TIMEZONE.
Identifies the generic SQL type TINYINT.
Identifies the generic SQL type VARBINARY.
Identifies the generic SQL type VARCHAR.
Method Summary
All Methods&&&&
Modifier and Type
Method and Description
Returns the SQLType name that represents a SQL data type.
Returns the name of the vendor that supports this data type.
Returns the vendor specific type number for the data type.
(int&type)
Returns the JDBCType that corresponds to the specified
Types value
Returns the enum constant of this type with the specified name.
Returns an array containing the constants of this enum type, in
the order they are declared.
Methods inherited from class&java.lang.
, , , , , , , , ,
Methods inherited from class&java.lang.
Enum Constant Detail
public static final& BIT
Identifies the generic SQL type BIT.
public static final& TINYINT
Identifies the generic SQL type TINYINT.
public static final& SMALLINT
Identifies the generic SQL type SMALLINT.
public static final& INTEGER
Identifies the generic SQL type INTEGER.
public static final& BIGINT
Identifies the generic SQL type BIGINT.
public static final& FLOAT
Identifies the generic SQL type FLOAT.
public static final& REAL
Identifies the generic SQL type REAL.
public static final& DOUBLE
Identifies the generic SQL type DOUBLE.
public static final& NUMERIC
Identifies the generic SQL type NUMERIC.
public static final& DECIMAL
Identifies the generic SQL type DECIMAL.
public static final& CHAR
Identifies the generic SQL type CHAR.
public static final& VARCHAR
Identifies the generic SQL type VARCHAR.
LONGVARCHAR
public static final& LONGVARCHAR
Identifies the generic SQL type LONGVARCHAR.
public static final& DATE
Identifies the generic SQL type DATE.
public static final& TIME
Identifies the generic SQL type TIME.
public static final& TIMESTAMP
Identifies the generic SQL type TIMESTAMP.
public static final& BINARY
Identifies the generic SQL type BINARY.
public static final& VARBINARY
Identifies the generic SQL type VARBINARY.
LONGVARBINARY
public static final& LONGVARBINARY
Identifies the generic SQL type LONGVARBINARY.
public static final& NULL
Identifies the generic SQL value NULL.
public static final& OTHER
Indicates that the SQL type
is database-specific and gets mapped to a Java object that can be
accessed via the methods getObject and setObject.
JAVA_OBJECT
public static final& JAVA_OBJECT
Indicates that the SQL type
is database-specific and gets mapped to a Java object that can be
accessed via the methods getObject and setObject.
public static final& DISTINCT
Identifies the generic SQL type DISTINCT.
public static final& STRUCT
Identifies the generic SQL type STRUCT.
public static final& ARRAY
Identifies the generic SQL type ARRAY.
public static final& BLOB
Identifies the generic SQL type BLOB.
public static final& CLOB
Identifies the generic SQL type CLOB.
public static final& REF
Identifies the generic SQL type REF.
public static final& DATALINK
Identifies the generic SQL type DATALINK.
public static final& BOOLEAN
Identifies the generic SQL type BOOLEAN.
public static final& ROWID
Identifies the SQL type ROWID.
public static final& NCHAR
Identifies the generic SQL type NCHAR.
public static final& NVARCHAR
Identifies the generic SQL type NVARCHAR.
LONGNVARCHAR
public static final& LONGNVARCHAR
Identifies the generic SQL type LONGNVARCHAR.
public static final& NCLOB
Identifies the generic SQL type NCLOB.
public static final& SQLXML
Identifies the generic SQL type SQLXML.
REF_CURSOR
public static final& REF_CURSOR
Identifies the generic SQL type REF_CURSOR.
TIME_WITH_TIMEZONE
public static final& TIME_WITH_TIMEZONE
Identifies the generic SQL type TIME_WITH_TIMEZONE.
TIMESTAMP_WITH_TIMEZONE
public static final& TIMESTAMP_WITH_TIMEZONE
Identifies the generic SQL type TIMESTAMP_WITH_TIMEZONE.
Method Detail
public static&[]&values()
Returns an array containing the constants of this enum type, in
the order they are declared.
This method may be used to iterate
over the constants as follows:
for (JDBCType c : JDBCType.values())
System.out.println(c);
an array containing the constants of this enum type, in the order they are declared
public static&&valueOf(&name)
Returns the enum constant of this type with the specified name.
The string must match exactly an identifier used to declare an
enum constant in this type.
(Extraneous whitespace characters are
not permitted.)
Parameters:
name - the name of the enum constant to be returned.
the enum constant with the specified name
- if this enum type has no constant with the specified name
- if the argument is null
public&&getName()
Returns the SQLType name that represents a SQL data type.
Specified by:
&in interface&
The name of this SQLType.
public&&getVendor()
Returns the name of the vendor that supports this data type.
Specified by:
&in interface&
The name of the vendor for this data type which is
java.sql for JDBCType.
getVendorTypeNumber
public&&getVendorTypeNumber()
Returns the vendor specific type number for the data type.
Specified by:
&in interface&
An Integer representing the data type. For JDBCType,
the value will be the same value as in Types for the data type.
public static&&valueOf(int&type)
Returns the JDBCType that corresponds to the specified
Types value
Parameters:
type - Types value
The JDBCType constant
- if this enum type has no constant with
the specified Types value
Java&&PlatformStandard&Ed.&8
For further API reference and developer documentation, see . That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
© , Oracle and/or its affiliates.
All rights reserved. Use is subject to . Also see the .
Scripting on this page tracks web page traffic, but does not change the content in any way.Read/Write JSON Using Jackson ObjectMapper, JsonParser and JsonGenerator Example
This page will describe how to read JSON
into Java object
and write java object into JSON output using Jackson API. Jackson has different API like ObjectMapper, JsonParser and JsonGenerator etc. We can read JSON from different resources like String variable, file or any network. ObjectMapper is most important class which acts as codec
or data binder.
ObjectMapper can
write java object into JSON file and read JSON file into java Object.
Jackson provides faster
i.e JsonParser
and JsonGenerator.
JsonParser reads JSON file
and JsonGenerator writes java object or map into JSON file using coded object.
While creating java class to map to and from JSON we must keep
a default constructor because Jackson API creates
java class instance using default constructor.
Here we will cover below points.
Gradle file to Resolve Jackson JAR Dependency
Find the Gradle file to resolve Jackson JAR dependency in our project.
build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
archivesBaseName = 'Concretepage'
version = '1'
repositories {
mavenCentral()
dependencies {
compile 'org.codehaus.jackson:jackson-mapper-asl:1.9.13'
Create JSON as String by Object using
ObjectMapper.writeValueAsString()
In Jackson JSON API org.codehaus.jackson.map.ObjectMapper
is an important class.
ObjectMapper
is a codec or data binder that maps java object
and JSON into each other. Here in this example we will convert java object into JSON string using
writeValueAsString() method of
ObjectMapper.
CreateJSONWithObjectMapper.java
package com.
import java.io.IOE
import org.codehaus.jackson.JsonGenerationE
import org.codehaus.jackson.map.JsonMappingE
import org.codehaus.jackson.map.ObjectM
public class CreateJSONWithObjectMapper {
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
Address address = new Address("Dhananjaypur", "Varanasi", "UP");
Person person = new Person(1, "Arvind", address);
String json = mapper.writeValueAsString(person);
System.out.println(json);
Find the output.
{"id":1,"name":"Arvind","address":{"village":"Dhananjaypur","district":"Varanasi","state":"UP"}}
Find the class that will be used to convert into JSON and read JSON into Java object. Find the Address class.
Address.java
package com.
public class Address {
public Address(){}
public Address(String village, String district, String state){
this.village =
this.district =
this.state =
public String getVillage() {
public void setVillage(String village) {
this.village =
public String getDistrict() {
public void setDistrict(String district) {
this.district =
public String getState() {
public void setState(String state) {
this.state =
Find the person class.
Person.java
package com.
public class Person {
public Person(){}
public Person(Integer id, String name, Address address){
this.name =
this.address =
public Integer getId() {
public void setId(Integer id) {
public String getName() {
public void setName(String name) {
this.name =
public Address getAddress() {
public void setAddress(Address address) {
this.address =
Write Object into JSON file using
ObjectMapper.writeValue()
In this example, we will create a java object and using ObjectMapper.writeValue() method,
will convert that java object into JSON file. writeValue() serializes
java object to JSON.
WriteJSONWithObjectMapperOne.java
package com.
import java.io.F
import java.io.IOE
import org.codehaus.jackson.JsonGenerationE
import org.codehaus.jackson.map.JsonMappingE
import org.codehaus.jackson.map.ObjectM
public class WriteJSONWithObjectMapperOne {
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
Address address = new Address("Dhananjaypur", "Varanasi", "UP");
Person person = new Person(1, "Arvind", address);
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(new File("D:/cp/dataOne.json"), person);
System.out.println("--Done--");
Find the output.
dataOne.json
{"id":1,"name":"Arvind","address":{"village":"Dhananjaypur","district":"Varanasi","state":"UP"}}
Pretty Print JSON using
ObjectWriter
with DefaultPrettyPrinter
Jackson API can write pretty print JSON . For that we can use DefaultPrettyPrinter instance.
WriteJSONWithObjectMapperTwo.java
package com.
import java.io.F
import java.io.IOE
import org.codehaus.jackson.JsonGenerationE
import org.codehaus.jackson.map.JsonMappingE
import org.codehaus.jackson.map.ObjectM
import org.codehaus.jackson.map.ObjectW
import org.codehaus.jackson.util.DefaultPrettyP
public class WriteJSONWithObjectMapperTwo {
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
Address address = new Address("Dhananjaypur", "Varanasi", "UP");
Person person = new Person(1, "Arvind", address);
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
writer.writeValue(new File("D:/cp/dataTwo.json"), person);
System.out.println("--Done--");
Find the output.
dataTwo.json
"name" : "Arvind",
"address" : {
"village" : "Dhananjaypur",
"district" : "Varanasi",
"state" : "UP"
Write java Map into JSON file using Jackson
ObjectMapper
If we have data into map, that can also be converted into JSON using
ObjectMapper.
WriteJSONWithObjectMapperThree.java
package com.
import java.io.F
import java.io.IOE
import java.util.HashM
import java.util.M
import org.codehaus.jackson.JsonGenerationE
import org.codehaus.jackson.map.JsonMappingE
import org.codehaus.jackson.map.ObjectM
import org.codehaus.jackson.map.ObjectW
import org.codehaus.jackson.util.DefaultPrettyP
public class WriteJSONWithObjectMapperThree {
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
Map&ltString,Object&gt person = new HashMap&ltString,Object&gt();
Map&ltString,String&gt address = new HashMap&ltString,String&gt();
address.put("Vill.", "Dhananjaypur");
address.put("Dist.", "Varanasi");
address.put("State", "UP");
person.put("id", "1");
person.put("name", "Arvind");
person.put("address", address);
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
writer.writeValue(new File("D:/cp/dataThree.json"), person);
System.out.println("--Done--");
Find the output.
dataThree.json
"address" : {
"Vill." : "Dhananjaypur",
"State" : "UP",
"Dist." : "Varanasi"
"name" : "Arvind",
"id" : "1"
Read JSON file into Object using ObjectMapper.readValue()
To read JSON file into java object, Jackson provides
ObjectMapper.readValue().
Find the input JSON file.
dataOne.json
{"id":1,"name":"Arvind","address":{"village":"Dhananjaypur","district":"Varanasi","state":"UP"}}
Now find the java class to read the JSON.
ReadJSONWithObjectMapper.java
package com.
import java.io.FileInputS
import java.io.IOE
import org.codehaus.jackson.JsonGenerationE
import org.codehaus.jackson.map.DeserializationC
import org.codehaus.jackson.map.JsonMappingE
import org.codehaus.jackson.map.ObjectM
import org.codehaus.jackson.map.SerializationC
public class ReadJSONWithObjectMapper {
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationConfig.Feature.AUTO_DETECT_FIELDS, true);
//read from string variable
String json ="{\"id\":1,\"name\":\"Arvind\",\"address\":{\"village\":\"Dhananjaypur\",\"district\":\"Varanasi\",\"state\":\"UP\"}}";
Person person = mapper.readValue(json, Person.class);
System.out.println("Name:"+person.getName()+", Vill."+ person.getAddress().getVillage());
//Read from file
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
person = mapper.readValue(new FileInputStream("D:/cp/dataOne.json"), Person.class );
System.out.println("Name:"+person.getName()+", Vill."+ person.getAddress().getVillage());
Find the output.
Name:Arvind, Vill.Dhananjaypur
Name:Arvind, Vill.Dhananjaypur
Write Object into JSON file using JsonGenerator
org.codehaus.jackson.JsonGenerator writes JSON
using codec.
JsonGenerator we can set codec as ObjectMapper() instance.
In this example we will convert java object into JSON file.
WriteJSONWithJsonGeneratorOne.java
package com.
import java.io.F
import java.io.FileOutputS
import java.io.IOE
import org.codehaus.jackson.JsonE
import org.codehaus.jackson.JsonF
import org.codehaus.jackson.JsonG
import org.codehaus.jackson.map.ObjectM
public class WriteJSONWithJsonGeneratorOne {
public static void main(String[] args) throws IOException {
JsonFactory jsonFactory = new JsonFactory();
FileOutputStream file = new FileOutputStream(new File("D:/cp/infoOne.json"));
JsonGenerator jsonGen = jsonFactory.createJsonGenerator(file, JsonEncoding.UTF8);
Address address = new Address("Dhananjaypur", "Varanasi", "UP");
Person person = new Person(1, "Arvind", address);
jsonGen.setCodec(new ObjectMapper());
jsonGen.writeObject(person);
System.out.println("Done");
Find the output.
infoOne.json
{"id":1,"name":"Arvind","address":{"village":"Dhananjaypur","district":"Varanasi","state":"UP"}}
Pretty Print using JsonGenerator
For pretty printing, Jackson provides DefaultPrettyPrinter that can be set to JsonGenerator instance.
WriteJSONWithJsonGeneratorTwo.java
package com.
import java.io.F
import java.io.FileOutputS
import java.io.IOE
import org.codehaus.jackson.JsonE
import org.codehaus.jackson.JsonF
import org.codehaus.jackson.JsonG
import org.codehaus.jackson.map.ObjectM
import org.codehaus.jackson.util.DefaultPrettyP
public class WriteJSONWithJsonGeneratorTwo {
public static void main(String[] args) throws IOException {
JsonFactory jsonFactory = new JsonFactory();
FileOutputStream file = new FileOutputStream(new File("D:/cp/infoTwo.json"));
JsonGenerator jsonGen = jsonFactory.createJsonGenerator(file, JsonEncoding.UTF8);
Address address = new Address("Dhananjaypur", "Varanasi", "UP");
Person person = new Person(1, "Arvind", address);
jsonGen.setCodec(new ObjectMapper());
jsonGen.setPrettyPrinter(new DefaultPrettyPrinter());
jsonGen.writeObject(person);
System.out.println("Done");
Find the output.
infoTwo.json
"name" : "Arvind",
"address" : {
"village" : "Dhananjaypur",
"district" : "Varanasi",
"state" : "UP"
Write java map into JSON
file using
JsonGenerator
Java map can be converted into JSON
file using JsonGenerator.
WriteJSONWithJsonGeneratorThree.java
package com.
import java.io.F
import java.io.FileOutputS
import java.io.IOE
import org.codehaus.jackson.JsonE
import org.codehaus.jackson.JsonF
import org.codehaus.jackson.JsonG
import org.codehaus.jackson.util.DefaultPrettyP
public class WriteJSONWithJsonGeneratorThree {
public static void main(String[] args) throws IOException {
JsonFactory jsonFactory = new JsonFactory();
FileOutputStream file = new FileOutputStream(new File("D:/cp/infoThree.json"));
JsonGenerator jsonGen = jsonFactory.createJsonGenerator(file, JsonEncoding.UTF8);
jsonGen.setPrettyPrinter(new DefaultPrettyPrinter());
jsonGen.writeStartObject();
jsonGen.writeNumberField("id", 1);
jsonGen.writeStringField("country", "India");
jsonGen.writeFieldName("states");
jsonGen.writeStartArray();
jsonGen.writeString("UP");
jsonGen.writeString("MP");
jsonGen.writeEndArray();
jsonGen.writeEndObject();
jsonGen.close();
System.out.println("Done");
Find the output.
infoThree.json
"country" : "India",
"states" : [ "UP", "MP" ]
Read JSON file using JsonParser and JsonNode
org.codehaus.jackson.JsonParser
reads JSON file.
JsonParser can read JSON as tree returning JsonNode.
Find the input JSON file.
infoTwo.json
"name" : "Arvind",
"address" : {
"village" : "Dhananjaypur",
"district" : "Varanasi",
"state" : "UP"
Now find the java class to read the JSON.
ReadJSONWithJsonParser.java
package com.
import java.io.F
import java.io.IOE
import java.util.I
import java.util.M
import org.codehaus.jackson.JsonF
import org.codehaus.jackson.JsonN
import org.codehaus.jackson.JsonParseE
import org.codehaus.jackson.JsonP
import org.codehaus.jackson.map.ObjectM
public class ReadJSONWithJsonParser {
public static void main(String[] args) throws JsonParseException, IOException {
JsonFactory jsonFactory = new JsonFactory();
JsonParser jp = jsonFactory.createJsonParser(new File("D:/cp/infoTwo.json"));
jp.setCodec(new ObjectMapper());
JsonNode jsonNode = jp.readValueAsTree();
readJsonData(jsonNode);
static void readJsonData(JsonNode jsonNode) {
Iterator&ltMap.Entry&ltString, JsonNode&gt&gt ite = jsonNode.getFields();
while(ite.hasNext()){
Map.Entry&ltString, JsonNode&gt entry = ite.next();
if(entry.getValue().isObject()) {
readJsonData(entry.getValue());
System.out.println("key:"+entry.getKey()+", value:"+entry.getValue());
Find the output.
key:id, value:1
key:name, value:"Arvind"
key:village, value:"Dhananjaypur"
key:district, value:"Varanasi"
key:state, value:"UP"
Download Complete Source Code
ARVIND RAI
Popular Tutorials:
FIND MORE TUTORILAS
Please enable JavaScript to view the
Subscribe for Latest Post
Latest Post
Find us on Facebook
Top Trends
Favorite Links
We are a group of software developers.
We enjoy learning and sharing technologies. To improve the site's content, your valuable suggestions are most welcome.
Mobile Apps

我要回帖

更多关于 map导出数据到数据库 的文章

 

随机推荐