Json解析丨简单制作

Json解析(一)


介绍

虽然我们的文档是json解析(一)但是也完全可以使用做json解析


提示:以下是本篇文章正文内容,下面案例可供参考

一、json文件展示

今天我们需要解析的文件内容为:

{
	"status": "ok",
	"results": [{
		"text": "\u96be\u53d7",
		"score": 0.9967435002326965,
		"position": [
			[267.0, 267.0],
			[412.0, 412.0],
			[412.0, 412.0],
			[267.0, 267.0]
		]
	}, {
		"text": "\u5ce8",
		"score": 0.7575550675392151,
		"position": [
			[62.0, 62.0],
			[142.0, 142.0],
			[142.0, 142.0],
			[62.0, 62.0]
		]
	}, {
		"text": "\u6211\u8fd8\u80fd\u8bf4\u4ec0\u516c",
		"score": 0.420401394367218,
		"position": [
			[475.0, 475.0],
			[662.0, 662.0],
			[662.0, 662.0],
			[475.0, 475.0]
		]
	}, {
		"text": "\u6ca1\u5fc5\u8981",
		"score": 0.9887173175811768,
		"position": [
			[52.0, 52.0],
			[160.0, 160.0],
			[160.0, 160.0],
			[52.0, 52.0]
		]
	}, {
		"text": "\u884c\u884c\u884c\u4f60\u8bf4\u4e86\u7b97",
		"score": 0.9628203511238098,
		"position": [
			[269.0, 269.0],
			[677.0, 677.0],
			[677.0, 677.0],
			[269.0, 269.0]
		]
	}, {
		"text": "\u5fc3\u5982\u6b7b\u7070",
		"score": 0.9149372577667236,
		"position": [
			[99.0, 99.0],
			[557.0, 557.0],
			[556.0, 556.0],
			[98.0, 98.0]
		]
	}, {
		"text": "690\u00d7624",
		"score": 0.8837266564369202,
		"position": [
			[616.0, 616.0],
			[683.0, 683.0],
			[683.0, 683.0],
			[616.0, 616.0]
		]
	}]
}

二、使用步骤

1.转换String为Json

其中我们的data是我们需要转换的字符串
responseJson则是给我们的文件序列化

        string responseString = data;
        JObject responseJson = JObject.Parse(responseString);

2.提取文字

获取“status”字段的值

        string status = (string)responseJson["status"];

获取“results”字段的值

JArray resultsArray = (JArray)responseJson["results"];
        List<JToken> results = resultsArray.ToList();
        

“results”列表中获取每个结果对象的值

 			JArray positionArray = (JArray)item["position"];
            List<Vector2> positionList = new List<Vector2>();
            foreach (JArray point in positionArray)
            {
                float x = (float)point[0];
                float y = (float)point[1];
                positionList.Add(new Vector2(x, y));
             

            }

3.成品代码展示

代码如下(示例):

void JsonText()
    {

        string responseString = data;
        JObject responseJson = JObject.Parse(responseString);
        //要获取“status”字段的值
        string status = (string)responseJson["status"];
        //要获取“results”字段的值
        JArray resultsArray = (JArray)responseJson["results"];
        List<JToken> results = resultsArray.ToList();
        //“results”列表中获取每个结果对象的值
        foreach (var item in results)
        {
            Debug.Log("text: " + item["text"]);
            Debug.Log("score: " + item["score"]);
            //texts.text += "text: " + item["text"]+"\n";
            //texts.text += "score: " + item["score"]+"\n";
            JArray positionArray = (JArray)item["position"];
            List<Vector2> positionList = new List<Vector2>();
            foreach (JArray point in positionArray)
            {
                float x = (float)point[0];
                float y = (float)point[1];
                positionList.Add(new Vector2(x, y));
                Debug.Log("x:  " + x + "   " + "y:  " + y);
                //texts.text += "x:  " + x+"\n";
                //texts.text += "y:  " + y+"\n";

            }

        }

4.效果

代码如下(示例):

在这里插入图片描述


总结

提示:这里对文章进行总结:

文章技能灵活使用,本文方法非常简单

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Glunn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值