map,对象,字符串转为json格式

本文介绍了如何将Map、对象以及字符串转换为JSON格式。首先展示了一个Map实例并利用JSONObject将其转换为JSON对象,然后展示了直接将对象转换为JSON的方法,最后解释了如何将包含键值对的字符串转换为JSON,注意字符串中的转义字符。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将map转为json

新建一个map

Map<String, String> map = new HashMap<String, String>();
 map.put("1","1");
 map.put("2","2");

使用JSONObject就可以将map直接转为json

package test;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;

public class Test {
	public static void main(String[] args) {
		demo1();
	}
	public static void demo1() {
		//map->json
		Map<String, String> map = new HashMap<String, String>();
		map.put("1","1");
		map.put("2","2");
		JSONObject json = new JSONObject(map);
		System.out.println(json);
		//{"1":"1","2":"2"}
		//json格式
	} 
}

输出结果为{"1":"1","2":"2"}

 

将对象转为json

直接使用JSONObject 来将对象转为json格式:JSONObject json = new  JSONObject(entity); 

新建一个对象

package dao;

public class Entity {
	String name;
	int age;
	Address address;
	public Entity() {
	}
	public Entity(String name, int age, Address address) {
		super();
		this.name = name;
		this.age = age;
		this.address = address;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "name" + " " + name + " " + "age";
	}

}
package dao;

public class Address {
	String a1;
	String a2;

	public Address() {
	}

	public Address(String a1, String a2) {
		super();
		this.a1 = a1;
		this.a2 = a2;
	}

	public String getA1() {
		return a1;
	}

	public void setA1(String a1) {
		this.a1 = a1;
	}

	public String getA2() {
		return a2;
	}

	public void setA2(String a2) {
		this.a2 = a2;
	}

}
package test;

import org.json.JSONObject;
import dao.Address;
import dao.Entity;

public class Test {
	public static void main(String[] args) throws IOException {
		demo2();
	}
	public static void demo2() {
		//javaBean->json
		Entity entity = new Entity();
		Address address = new Address("a1","a2"); 
		entity.setAddress(address);
		entity.setAge(15);
		entity.setName("s");
		JSONObject json = new  JSONObject(entity); 
		System.out.println(json);
		//{"address":{"a1":"a1","a2":"a2"},"name":"s","age":15}
	}
	
}

 

将字符串转为json

在字符串中要写成键值对的形式,如果是字符串要有转义字符

String  str = "{\"name\":\"zs\",\"age\":23}"; 

package test;

import org.json.JSONObject;

public class Test {
	public static void main(String[] args){
		demo3();
	}
	public static void demo3() {
		String  str = "{\"name\":\"zs\",\"age\":23}"; 
		JSONObject json = new JSONObject(str);
		System.out.println(str);
		//{"name":"zs","age":23}
	}
}

 

要将Excel格式字符串转换为JSON,您可以使用Java中的Apache POI库来读取Excel文件并从中提取数据,然后将数据转换为JSON格式。下面是一个简单的示例代码片段,它演示了如何将Excel文件中的数据转换为JSON格式: ```java import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import com.fasterxml.jackson.databind.ObjectMapper; public class ExcelToJsonConverter { public static void main(String[] args) { try { // Load Excel file FileInputStream file = new FileInputStream(new File("input.xlsx")); Workbook workbook = WorkbookFactory.create(file); // Read Excel sheet and convert to JSON Sheet sheet = workbook.getSheetAt(0); List<Map<String, String>> list = new ArrayList<Map<String, String>>(); for (Row row : sheet) { Map<String, String> map = new HashMap<String, String>(); for (Cell cell : row) { map.put(cell.getStringCellValue(), cell.getStringCellValue()); } list.add(map); } ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(list); System.out.println(json); // Close file file.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 在上面的代码中,我们首先使用Apache POI库加载Excel文件,然后使用WorkbookFactory创建工作簿。接下来,我们选择第一个工作表并将其转换为JSON格式。最后,我们使用Jackson库的ObjectMapper将List<Map<String, String>>对象转换为JSON字符串
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值