如何将对象转换成js object对象转数组

137910人阅读
JavaScript(13)
利用原生JSON对象,将对象转为字符串
var jsObj = {};
jsObj.testArray = [1,2,3,4,5];
jsObj.name = 'CSS3';
jsObj.date = '8 May, 2011';
var str = JSON.stringify(jsObj);
alert(str);
从JSON字符串转为对象
var jsObj = {};
jsObj.testArray = [1,2,3,4,5];
jsObj.name = 'CSS3';
jsObj.date = '8 May, 2011';
var str = JSON.stringify(jsObj);
var str1 = JSON.parse(str);
alert(str1);
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:314209次
积分:2214
积分:2214
排名:第12688名
原创:52篇
评论:28条
(15)(1)(1)(6)(8)(19)(1)(5)温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
1.&&重写Student类中的toString()和equals()方法;
2.写类Animal,Dog,Cat,Test,Dog和Cat继承Animal,Animal有属性name,Dog有属性color,Cat有weight属性,Test中有一个方法public void function(Animal a),调用该方法,首先输出传入对象的name,然后判断传入的是Dog还是Cat,并输出其自身特有的属性
package com.bluedot.
public class Student {&private S&&private S&&public Student(String name, int age, String degree) {&&this.name =&&this.age =&&this.degree =&}&&public String getName() {&&&}&public void setName(String name) {&&this.name =&}&public int getAge() {&&&}&public void setAge(int age) {&&this.age =&}&public String getDegree() {&&&}&public void setDegree(String degree) {&&this.degree =&}&&public void info() {&&System.out.println("姓名:" + name + " 年龄:" + age + " 学位:" + degree);&}
&@Override&public String toString() {&&return "姓名:" + name + " 年龄:" + age + " 学位:" +&}
&@Override&public boolean equals(Object obj) {&&if(obj!=null){&&&if(obj instanceof Student&&!(obj instanceof Graduate)&&!(obj instanceof Undergraduate)){&&&&Student s = (Student)&&&&if(s.getName()==name&&s.getAge()==age&&s.getDegree()==degree){&&&&&&&&&}else{&&&&&&&&&}&&&}else{&&&&&&&}&&}&&&}&package com.bluedot.
public class Animal {&private S&&public Animal(String name){&&this.name =&}&&public String getName() {&&&}
&public void setName(String name) {&&this.name =&}&}
package com.bluedot.
public class Cat extends Animal{&&&public Cat(String name, int weight){&&super(name);&&this.weight =&}
&public int getWeight() {&&&}
&public void setWeight(int weight) {&&this.weight =&}&&}
package com.bluedot.
public class Dog extends Animal {&private S&&public Dog(String name, String color){&&super(name);&&this.color =&}
&public String getColor() {&&&}
&public void setColor(String color) {&&this.color =&}&&}
package com.bluedot.
public class Test {&public static void main(String[] args){//&&Student s = new Student("张三", 25, "bachelor");//&&Student s1 = new Student("张三", 25, "bachelor");//&&System.out.println(s.toString());//&&System.out.println(s.equals(s1));&&&&Animal animal = new Animal("动物");&&Dog dog = new Dog("黄黄","yellow");&&Cat cat = new Cat("甜甜",10);&&Test t = new Test();&&t.function(animal);&&t.function(dog);&&t.function(cat);&}&&public void function(Animal a){&&System.out.println(a.getName());&&if(a instanceof Dog){&&&Dog d = (Dog)a;&&&System.out.println(d.getColor());&&}else if(a instanceof Cat){&&&Cat c = (Cat)a;&&&System.out.println(c.getWeight());&&}&}}
阅读(3552)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
loftPermalink:'',
id:'fks_',
blogTitle:'java学习(object两个常用方法,对象的转型)',
blogAbstract:'Object类中定义有:public '
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}推荐这篇日记的豆列
······

我要回帖

更多关于 c object 转对象 的文章

 

随机推荐