stringToZSON(string text)
说明:json 字符串转为ZSONObject对象
参数
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| text | string | 是 | 传入json字符串 |
示例
ZSONObject zsonObject= ZSONObject.stringToZSON(jsonStr); toString():
说明:ZSONObject对象转化为json字符串
返回值
| 类型 | 说明 |
|---|---|
| String | 返回的json字符串 |
示例
String jsonStr=jsonObject.toString();
put
public Object put(String key, Object value)
说明:将指定的键值对放置到此ZSONObject的映射中。
| 类型 | 说明 |
|---|---|
| key | 表示要存储在此ZSONObject的映射中的key。 |
| value | 表示要存储在此ZSONObject的映射中的value。 |
示例
jsonObject.put("key","value"); get
public Object get(Object key)
获取此ZSONObject映射中指定键匹配的对象。
| 类型 | 说明 |
|---|---|
| key | 表示存储在此ZSONObject映射中的key。 |
Object Value= zsonObject.get("key");
准备的工作
创建一个User类,包含Name和Age的属性,代码如下
- package com.harmony.alliance.myapplication.model;
-
- public class User {
- private String name;
- private int age;
-
- @Override
- public String toString() {
- return "User{" +
- "name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
- }
xml布局
在xml布局上绘画三个Text组件,分别用于java对象转化json、json字符串转化为user对象,显示结果,代码如下
- <DirectionalLayout
- ohos:id="$+id:directionlayout"
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="match_parent"
- ohos:width="match_parent"
- ohos:alignment="center|top"
- ohos:orientation="vertical">
- <Text
- ohos:id="$+id:toJson"
- ohos:height="100vp"
- ohos:width="match_parent"
- ohos:layout_alignment="horizontal_center"
- ohos:multiple_lines="true"
- ohos:text_size="40vp"
- ohos:text="对象转化为json字符串"
- />
- <Text
- ohos:id="$+id:parseJson"
- ohos:height="100vp"
- ohos:width="match_parent"
- ohos:layout_alignment="horizontal_center"
- ohos:multiple_lines="true"
- ohos:text_size="40vp"
- ohos:text="json转化为对象"
- ohos:background_element="#ed6262"
- />
-
- <Text
- ohos:id="$+id:result"
- ohos:height="match_parent"
- ohos:width="match_parent"
- ohos:layout_alignment="horizontal_center"
- ohos:multiple_lines="true"
- ohos:text_size="40vp"
- ohos:text="显示结果"
- />
-
-
- DirectionalLayout>
效果如下

java 代码
第一步:实现text点击功能对java对象怎么转化为json字符串,代码如下
- mTextToJson.setClickedListener(new Component.ClickedListener() {
- @Override
- public void onClick(Component component) {
- //初始化User 对象,并且对user实现实例化
- ZSONObject jsonObject = new ZSONObject();
- jsonObject.put("name","luck");
- jsonObject.put("age",18);
- jsonStr=jsonObject.toString();
- mTextResult.setText("显示结果:"+jsonStr);
- }
- });
第二步:实现json字符串转化为user对象,代码如下
- mTextParseJson.setClickedListener(new Component.ClickedListener() {
- @Override
- public void onClick(Component component) {
- ZSONObject zsonObject= ZSONObject.stringToZSON(jsonStr);
- String name= (String) zsonObject.get("name");
- int age=zsonObject.getInteger("age");
- //todo 初始化User 对象,然后封装为user 对象
- User user=new User();
- user.setName(name);
- user.setAge(age);
- mTextResult.setText("json转化为User对象显示结果:"+user.toString());
- }
- });
java全部代码
- package com.harmony.alliance.myapplication.slice;
-
- import com.harmony.alliance.myapplication.ResourceTable;
- import com.harmony.alliance.myapplication.model.User;
- import ohos.aafwk.ability.AbilitySlice;
- import ohos.aafwk.content.Intent;
- import ohos.agp.components.Component;
- import ohos.agp.components.Text;
- import ohos.utils.zson.ZSONObject;
- public class MainAbilitySlice extends AbilitySlice {
- private Text mTextToJson,mTextParseJson,mTextResult;
- private String jsonStr;
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- super.setUIContent(ResourceTable.Layout_ability_main);
- mTextToJson=findComponentById(ResourceTable.Id_toJson);
- mTextParseJson=findComponentById(ResourceTable.Id_parseJson);
- mTextResult=findComponentById(ResourceTable.Id_result);
- mTextToJson.setClickedListener(new Component.ClickedListener() {
- @Override
- public void onClick(Component component) {
- //初始化User 对象,并且对user实现实例化
- ZSONObject jsonObject = new ZSONObject();
- jsonObject.put("name","luck");
- jsonObject.put("age",18);
- jsonStr=jsonObject.toString();
- mTextResult.setText("显示结果:"+jsonStr);
- }
- });
- mTextParseJson.setClickedListener(new Component.ClickedListener() {
- @Override
- public void onClick(Component component) {
- ZSONObject zsonObject= ZSONObject.stringToZSON(jsonStr);
- String name= (String) zsonObject.get("name");
- int age=zsonObject.getInteger("age");
- //todo 初始化User 对象,然后封装为user 对象
- User user=new User();
- user.setName(name);
- user.setAge(age);
- mTextResult.setText("json转化为User对象显示结果:"+user.toString());
- }
- });
-
-
-
- }
-
- }
运行效果

欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh