当前位置:首页>开发>正文

javascript把JSON对象数组转换成JSON字符串 json数组怎么转json字符串

2023-07-05 08:37:42 互联网 未知 开发

 javascript把JSON对象数组转换成JSON字符串 json数组怎么转json字符串

javascript把JSON对象数组转换成JSON字符串

add方法改成

function add(id,name,age){
humens.push({
id: id,
name: name,
age: age
})
}

原因是你push进去的是function
function是不可序列化的, stringify之后会变成null

json数组怎么转json字符串

// 转换成对象

var myObject = JSON.parse(_data)
alert("对象:" myObject.msg)

// 转换成数组
var myobj = eval("[" _data "]")
for (var i = 0 i < myobj.length i ) {
alert("数组:" myobj[i].msg)
}

//把json格式的字符串转成JSON对象

var json1 = {"department": "中兵","address": "北京","contacts": "李硕","telphone": "15201274666","email": "li_shilei@yeah.NET","fax": "0108596666"}
var json2 = {"department": "中兵2","address": "北京2","contacts": "李硕2","telphone": "152012746662","email": "li_shilei2@yeah.Net","fax": "01085966662"}
var jsonObj = $.parseJSON(json1) 或用eval方法

//把数组转成JSON对象

怎样将json数组装成json字符串

JS 字面量对象 转成 json 字符串
var arr = [1,2,3, { a : 1 } ]
JSON.stringify( arr )

字符串解析成 JSON
var str = [1,2,3,{"a":1}]
JSON.parse( str )

怎么把json对象转换为json字符串

json-lib这个类库,方便的支持java中对象到json字符串的转化。
看source-forge的json-lib介绍说,需要
jakarta commons-lang 2.4
jakarta commons-beanutils 1.7.0
jakarta commons-collections 3.2
jakarta commons-logging 1.1.1
ezmorph 1.0.6
jar包下载:java2json.jar
这些jar包的支持,下载下来试了一把,挺好用的,写出来,与大家分享。
代码如下:
import java.util.ArrayList
import java.util.HashMap
import java.util.List
import java.util.Map

import net.sf.json.JSONArray
import net.sf.json.JSONObject

public class Test {
public static void main(String[] args) {
//当是对象的时候
Student student = new Student()
student.setAge(18)
student.setName("zhangsan")
student.setSex("male")
JSONObject jsonObject = JSONObject.fromObject(student)
System.out.println(jsonObject)//输出{"age":18,"name":"zhangsan","sex":"male"}
//当是数组或list的时候
Student[] stus = new Student[5]
List stuList = new ArrayList()
for (int i = 0 i < stus.length i ) {
stus[i] = new Student()
stus[i].setAge(i*10 8)
stus[i].setName("zhang" i)
stus[i].setSex("male")
//添加到list,一会儿用
stuList.add(stus[i])
}
JSONArray jsonArray = JSONArray.fromObject(stus)
System.out.println(jsonArray)//和下面打印的结果相同
// 结果:[{"age":8,"name":"zhang0","sex":"male"}, {"age":18,"name":"zhang1","sex":"male"},{"age":28,"name":"zhang2","sex":"male"},{"age":38,"name":"zhang3","sex":"male"},{"age":48,"name":"zhang4","sex":"male"}]
JSONArray jsonArrayFromList = JSONArray.fromObject(stuList)
System.out.println(jsonArrayFromList)//和上面打印的结果相同
//当既有对象又有数组的时候
Map map = new HashMap()
Teacher teacher = new Teacher()
teacher.setAge(30)
teacher.setName("laoshi")
teacher.setSex("male")
map.put("teacher", teacher)
map.put("student", stuList)
JSONObject jsonObjectFromMap = JSONObject.fromObject(map)
System.out.println(jsonObjectFromMap)
//结果:
//{"student":[{"age":8,"name":"zhang0","sex":"male"},{"age":18,"name":"zhang1","sex":"male"},{"age":28,"name":"zhang2","sex":"male"},{"age":38,"name":"zhang3","sex":"male"},{"age":48,"name":"zhang4","sex":"male"}],"teacher":{"age":30,"name":"laoshi","sex":"male"}}
}
}

关于C#使用json,不能把匿名类对象转成json字符串吗

根据你的json数据,可以像下面方式进行转换:

调用Custom 类中的 DeJson方法 传入json字符串 返回对象

追问:如果照这样定义,custom永远就只有两个chapter,如果json中有list3,就不行了。追答:如果不是固定的类,可以使用Newtonsoft中提供的JObject对象,但是如果说你还是希望反序列化成对象的话,那么 你这个格式应该是固定的【{title:1,imgurl:,articleurl:}
】 不然你怎么反序列化成对象?
如下图片:
0
0

0