search
HomeComputer TutorialsComputer KnowledgeConvert JSON data to array form

Convert JSON data to array form

Convert JSON data to array form

var data = [{

"a": "1",

"b": "2",

"c": "3 4 5 6 7"

}];

for(var i = 0; i

var obj = data[i];

for(var k in obj){

var arr = obj[k].split(/\s /);

if(arr.length > 1){

for(var j = 0; j

obj[k "_" (j 1)] = arr[j];

}

delete(obj[k]);

}

}

}

console.log(data);

java解析json数据成数组

import net.sf.json.JSONArray;

public class TestJson

{

public static void main(String []args)

{

String json = "[{\"a\":\"111\",\"b\":\"222\",\"c\":\"333\"},{\"a\":\"1000\",\"b\":\"2000\",\"c\":\"000\"},{\"a\":\"999\",\"b\":\"300\",\"c\":\"700\"}]";

JSONArray jsonArr = JSONArray.fromObject(json);

String a[] = new String[jsonArr.size()];

String b[] = new String[jsonArr.size()];

String c[] = new String[jsonArr.size()];

for (int i = 0; i

a[i] = jsonArr.getJSONObject(i).getString("a");

b[i] = jsonArr.getJSONObject(i).getString("b");

c[i] = jsonArr.getJSONObject(i).getString("c");

}

for (int i = 0; i

System.out.print(a[i] " ");

System.out.print(b[i] " ");

System.out.print(c[i]);

System.out.println();

}

}

}

java怎么把数据转成json

有两种方法实现:

(1)直接转换:

创建JSONArray对象、JSONObject对象,通过put方法将其数据以键值对的形式填充,如例:

//假如有一个Java实体类Person,包括以下属性:phone、sex、married、address。

ArrayList persons = new ArrayList();

for (int i = 0; i

persons.add(new Person("138******67", "女", true, "北京朝阳区"));//填充Java实体类集合

}

JSONArray array = new JSONArray();// Json格式的数组形式

JSONObject obj;//json格式的单个对象形式

for (int i = 0; i

obj = new JSONObject();

//依次填充每一个Java实体对象

try {

obj.put("phone", persons.get(i).getPhone());//json通过put方式以key-value形式填充

obj.put("sex", persons.get(i).getSex());

obj.put("married", persons.get(i).getMarried());

obj.put("address", persons.get(i).getAddress());

array.put(obj);//将JSONObject添加入JSONArray

} catch (JSONException e) {

e.printStackTrace();

}

}

(2)通过GSON工具实现:

只需要创建GSON对象,通过toJson方法转换即可。如例:

//模拟Java数据

Person person = new Person("敏敏", 23, "女", "北京市海淀区");

//创建GSON对象

Gson gson = new Gson();

//调用toJson方法即可转换为字符串的json格式

String json = gson.toJson(person);

The above is the detailed content of Convert JSON data to array form. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:Excel办公网. If there is any infringement, please contact admin@php.cn delete
How to Check if Your PC Is Compatible with Windows 11 22H2? - MiniToolHow to Check if Your PC Is Compatible with Windows 11 22H2? - MiniToolMay 16, 2025 am 01:57 AM

Windows 11 22H2 is around the corner. Dow you know how to check if your PC is compatible with Windows 22H2? In this post, php.cn Software will tell you how to do this using Registry Editor. No matter you are currently running Windows 10 or Windows 11

What Is Backup Encryption? Should You Encrypt Backups & How? - MiniToolWhat Is Backup Encryption? Should You Encrypt Backups & How? - MiniToolMay 16, 2025 am 01:54 AM

What does encrypt backups mean on Windows? Is it possible to encrypt backups? Should you encrypt backups? How do I encrypt a backup file? Today, php.cn will focus on backup encryption in this post, and let’s look through this guide to find what you n

How Old Do You Have to Be to Have Snapchat? The Answer is 13! - MiniToolHow Old Do You Have to Be to Have Snapchat? The Answer is 13! - MiniToolMay 16, 2025 am 01:53 AM

How old do you have to be for Snapchat? Officially, you should be at least 13 to be able to use Snapchat and with your parent’s permission if you are under 18. This essay disserted by php.cn Software explains more details for you.

The Nintendo Switch OLED: Release Date, Price, Specs & Games - MiniToolThe Nintendo Switch OLED: Release Date, Price, Specs & Games - MiniToolMay 16, 2025 am 01:50 AM

The Nintendo Switch OLED is a bestselling game console. If you want to purchase the Nintendo Switch OLED, you may want to learn the Nintendo Switch OLED price, release date, specs, and games. In this post, php.cn will introduce detailed information a

How to Enable Tabs in File Explorer on Your Windows 11 PC? - MiniToolHow to Enable Tabs in File Explorer on Your Windows 11 PC? - MiniToolMay 16, 2025 am 01:47 AM

The new File Explorer tabs feature is introduced in some devices in the Beta and Dev Channels. However, this new feature is not widely spread. If it is not available on your device, you can use the methods introduced in this php.cn post to manually e

Windows 11 Build 25131 Released with Bug Fixes & Updated Store - MiniToolWindows 11 Build 25131 Released with Bug Fixes & Updated Store - MiniToolMay 16, 2025 am 01:42 AM

If you are a Windows Insider in the Dev Channel, you can now download and install a new Windows 11 build for use. This latest build is Windows 11 build 25131. This build doesn’t have any new features but it contains some bug fixes and an updated Micr

How to Download Internet Download Manager, Install & Use IDM - MiniToolHow to Download Internet Download Manager, Install & Use IDM - MiniToolMay 16, 2025 am 01:39 AM

What is Internet Download Manager (IDM)? Can you get IDM for free? How to download IDM and install it on your Windows 11/10/8/7 PC for downloading something? It is easy and just find details from this guide from php.cn.

How to Check Software Version on Android/iPhone/Windows 10? - MiniToolHow to Check Software Version on Android/iPhone/Windows 10? - MiniToolMay 16, 2025 am 01:36 AM

How to check software version on PC/phone? php.cn Website offers you this guide to enable you to quaint that. You can learn software versions from all aspects, such as definition, types, checking methods on different devices, etc.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Clair Obscur: Expedition 33 - How To Get Perfect Chroma Catalysts
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools