Type Conversion in Java and JS (Jsonarray, JsonObject, JSON)

tags: Java  front end  json  javascript  

1, Java List <map <string, object >> Type Corresponding to the object array in JS
<%@page import="net.sf.json.JSONArray"%>
List<Map<String, Object>> dataList = xxxxx;
// js code
var allCollectedRecords =<%=JSONArray.fromObject(dataList)%>;   // [{"OID":"12345","NAME":"test"},{"OID":"659","NAME":"yuwang"}]
var refMap = <%=JSONArray.fromObject(dataList).getJSONObject(0)%>;
var OID = refMap["OID"];
2, JSON character string turn object in Java
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

String data = (String)params.get("data");    // "[{\"OID\":\"12345\",\"NAME\":\"test\"},{\"OID\":\"659\",\"NAME\":\"yuwang\"}]"
JSONArray jsonArray=JSON.parseArray(data);
JSONObject partObj = jsonArray.getJSONObject(0);
String partOid = (String) partObj.get("OID");
3, JS object turns JSON string
var obj = {"name":"test", "site":"aaaaa"}
var str = JSON.stringify(obj);
alert(str);

Intelligent Recommendation

Use JSONObject entity class, and Json Json string type conversion (java)

It is usedcom.alibaba.fastjson.JSONObject JSONObject the maven-dependent: People.java JSONObjectTest.java test class Print Console as follows: to sum up Entity Object convert Json string class using J...

JSONObject and JSONArray in json-lib

 Json-lib is used to complete the transformation of beans, collections, maps, java arrays, XML and JSON between jars, json-lib depends on several other jar packages (I tried all jars with the latest v...

Json,JsonObject,JsonArray,Bean

concept String: This well explained, referring to the use of "" or double quotes '' comprises a single quote character. E.g: String comStr = "this is string; " Json: refers to the ...

More Recommendation

Json learning JSONObject and JSONArray

1  JSONObject Add a jsonobject Print results E / ===== jsonObject: { "age": "23", "hobby": "Game", "name": "Komaki"} 1.1 acquisition va...

json,jsonObject,jsonArray

Write a directory title here JsonObject source code jsonArray JsonObject source code jsonArray...

JSON analysis JSONOBJECT and JSONARRAY

First, application scenario 1. The java class in the background, such as solid classes, list, set, map to the JSON string to be sent to the front or other items 2. JSON string sent by the front or oth...

JSON, JSONOBJECT, JSONARRAY in Fastjson

content Introduction demo Introduction demo plant test   Output result     Refer...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top