android中json的拼装与解析

android中json的拼装与解析

java 层使用的库为 org.json

首先导入以下package

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONArray;

构建json文本


int sequenceId = 1;
String payload = "xxxxx";
try {
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("sequenceId", sequenceId);
    jsonObject.put("payload", payload);
    jsonArray = new JSONArray();
    jsonArray.put(value1);
    jsonArray.put(value2);
    jsonArray.put(value2);
} catch (JSONException e) {
    e.printStackTrace();
}
jsonObject.toString();
String.valueOf(jsonObject);

getType和optType 的使用
getType将要获取的键的值转换为指定的类型,如果无法转换或没有值则抛出JSONException;
optType将要获取的键的值转换为指定的类型,无法转换或没有值时返回用户提供或这默认提供的值;

try {
    JSONObject jsonObject = new JSONObject(jsonStr);
    sequenceId = jsonObject.getInt("sequenceId");
    payload = jsonObject.optString("payload");
} catch (JSONException e) {
    e.printStackTrace();
    System.out.println(String.valueOf(e));
}

获取 JSONArray 对象并解析
使用 jsonObject.optJSONArray(key) 方法
String dtcCodeResult = "";
int dtcCodeCount = 0;
Number tmpdtccodevalue = 0;
JSONArray jsonArray;
try {
    jsonObject = new JSONObject("{\"Code\":[65416,65417,65418,65419,65420,65421]}");
    jsonArray = jsonObject.optJSONArray("Code");
    dtcCodeCount = jsonArray.length();
    dtcCodeResult = "";
    for (int i = 0; i < dtcCodeCount; i++) {
        tmpdtccodevalue = (Number)jsonArray.get(i);
        dtcCodeResult += Integer.toUnsignedString(tmpdtccodevalue.intValue(), 16).toUpperCase();
        if (i < dtcCodeCount-1) {
            dtcCodeResult += ",";
        }
    }
} catch (JSONException e) {
    e.printStackTrace();
    System.out.println(String.valueOf(e));
}

使用 new JSONArray() 构造方法
String dtcCodeResult = "";
int dtcCodeCount = 0;
Number tmpdtccodevalue = 0;
String tmpdtccodestr;
JSONArray jsonArray;
List<String> mInfo = new ArrayList<>();
try {
    jsonArray = new JSONArray("[65416,65417,65418,65419,65420,65421]");
    dtcCodeCount = jsonArray.length();
    dtcCodeResult = "";
    for (int i = 0; i < dtcCodeCount; i++) {
        tmpdtccodevalue = (Number)jsonArray.get(i);
        tmpdtccodestr = Integer.toUnsignedString(tmpdtccodevalue.intValue(), 16).toUpperCase();
        mInfo.add(tmpdtccodestr);
    }
} catch (JSONException e) {
    e.printStackTrace();
    System.out.println(String.valueOf(e));
}


Cpp 使用 libjsoncpp 库
#include <json/json.h>

解析
std::string jiStr;
Json::Value jiRoot;
Json::Reader jReader {};
if (!jReader.parse(jiStr, jiRoot, /*collectComments=*/false))
{
    return false;
}
return true;

std::string key;
Json::Value value {};
Json::Value defaultvalue {};
value = jiRoot.get(key, defaultvalue);

if (!jtmp.isNull()) {}
if (jtmp.isObject()) {Json::Value value = jtmp;}
if (jtmp.isArray()) {Json::Value value = jtmp;}
if (jtmp.isBool()) {bool value = jtmp.asBool();}
if (jtmp.isDouble()) {double value = jtmp.asDouble();}
if (jtmp.isInt()) {int value = jtmp.asInt();}
if (jtmp.isString()) {std::string value = jtmp.asString();}

拼装
Json::FastWriter jWriter;
Json::Value joRoot;
std::string joStr;

joRoot[key1] = Json::Value((int)intvalue);
joRoot[key2] = Json::Value(std::to_string(strvalue));
joRoot[key3] = Json::Value((bool)Mode);
joRoot[Number] = Json::Value((const char *)phoneNumber);
joStr = jWriter.write(joRoot);

JSONArray 的拼装
Json::FastWriter jWriter;
Json::Value joRoot;
std::string joStr;

uint32_t iLoop = 0;
int buff[] = {65416,65417,65418,65419,65420,65421};
uint32_t dtcBuffSize = sizeof(buff)/sizeof(buff[0]);;
for (iLoop = 0; iLoop < dtcBuffSize; iLoop++) {
    joRoot["Code"].append(buff[iLoop]);
}
joStr = jWriter.write(joRoot);
// now joStr is {\"Code\":[65416,65417,65418,65419,65420,65421]}
 

猜你喜欢

转载自blog.csdn.net/halazi100/article/details/106220135
今日推荐