日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

com.fasterxml.jackson.databind.ObjectMapper

作者:spencer_tseng 更新時(shí)間: 2023-12-07 編程語(yǔ)言

package zwf;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 
https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind/2.8.7

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.7</version>
</dependency>
 *
 * @author ZengWenFeng
 * @date 2023.11.27
 * @email 117791303@qq.com
 * @mobile 13805029595
 */

public class Test2Json
{

	public Test2Json()
	{
		
	}

	public static void main(String[] args)
	{
		// 包含制表符的文本數(shù)據(jù)
		String tabSeparatedData = "name\tage\tcity\nJohn\t25\tNew York\nAlice\t30\tChicago";

		// 將制表符文本數(shù)據(jù)轉(zhuǎn)換為JSON
		String[] lines = tabSeparatedData.split("\n");
		String[] headers = lines[0].split("\t");

		// 創(chuàng)建一個(gè)ObjectMapper對(duì)象
		ObjectMapper objectMapper = new ObjectMapper();

		// 創(chuàng)建一個(gè)空的JSON數(shù)組
		List<Object> jsonArray = new ArrayList<Object>();

		// 遍歷文本數(shù)據(jù)的每一行,將其轉(zhuǎn)換為JSON對(duì)象
		for (int i = 1; i < lines.length; i++)
		{
			String[] values = lines[i].split("\t");
			Map<String, String> jsonMap = new HashMap<String, String>();

			// 遍歷每個(gè)字段,將其添加到JSON對(duì)象中
			for (int j = 0; j < headers.length; j++)
			{
				jsonMap.put(headers[j], values[j]);
			}

			// 將JSON對(duì)象添加到JSON數(shù)組中
			jsonArray.add(jsonMap);
		}

		// 將JSON數(shù)組轉(zhuǎn)換為JSON字符串
		try
		{
			String jsonOutput = objectMapper.writeValueAsString(jsonArray);
			System.out.println(jsonOutput);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

}

原文鏈接:https://blog.csdn.net/spencer_tseng/article/details/134656783

  • 上一篇:沒(méi)有了
  • 下一篇:沒(méi)有了
欄目分類
最近更新