应当把json怎么解析出来呢?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了应当把json怎么解析出来呢?相关的知识,希望对你有一定的参考价值。
sm.SkipAuth = true;
sm.UseTitle = false;
//正式
string ncurl = "http://183.237.82.197:36105/uapws/rest/mwx/token/getToken.json";
JObject obj = new JObject(
new JProperty("data", new JObject(
new JProperty("userName", "NC65"),
new JProperty("pwd", "t1toncrestful")
)));
var headers = new System.Collections.Hashtable();
headers["Content-Type"] = "application/json";
string tokens = sm.HttpPost(ncurl, obj.ToString(), headers);
string token = String.Empty;
JObject tokenObj = JObject.Parse(tokens);
if (tokenObj["state"].ToString().Equals("success"))
token = tokenObj["result"].ToString();
// tm.SetMessage("获取token成功" + token);
//正式环境http://ip:port/uapws/rest/mwx/itf/getCustBillDetal.json
string newurl = "http://183.237.82.197:36105/uapws/rest/mwx/itf/getCustBillDetal.json";
JObject newobj = new JObject(
new JProperty("data", new JObject(
new JProperty("orgCode", "103"),
new JProperty("startDate", "2019-07-01"),
new JProperty("endDate", "2019-07-30"),
new JProperty("customerCode", "GDGZ35"),
new JProperty("deptCode", "06"),
new JProperty("billStatus", "1")
)));
headers = new System.Collections.Hashtable();
headers["Content-Type"] = "application/json";
headers["token"] = token;
string msg = sm.HttpPost(newurl, newobj.ToString(), headers);
JObject retobj = JObject.Parse(msg);
tm.SetMessage("提示信息" + retobj.ToString());
return true;
c#中怎么解析多层json数据
参考技术A 在.net 2.0中提取这样的json"name":"lily","age":23,"addr":"city":guangzhou,"province":guangdong
引用命名空间
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
可以把上面的JSON看成一个对象.你只要写对应的类即可
public class UserInfo
public string name;
public int age;
public address addr;
public class address
public string city;
public string province;
然后在解析的地方这样写:
string jsonData="\"name\":\"lily\",\"age\":23,\"addr\":\"city\":guangzhou,\"province\":guangdong";
UserInfo user=(UserInfo)JsonConvert.DeserializeObject(jsonData, typeof(UserInfo));
得到City的值只要:user.addr.City;
这样实现也行
JObject jsonObj = JObject.Parse(jsonData);
string name=jsonObj ["name"].ToString();
string age=jsonObj ["age"].ToString();
string city=((JObject )jsonObj ["addr"])["city"].ToString();
string province=((JObject )jsonObj ["addr"])["province"].ToString();
如 何这个json是动态的呢?譬如让你输入一个json,如"name":"lily","age":23,"addr": "city":guangzhou,"province":guangdong; 然后让你输入一个对象,如city,然后系统会输出guangzhou这个值,那这样的话,json就是动态生成的了,我想了解有没有读取这样的json 的方法。(注意,json是多级嵌套的。)
就用遍历
public string GetJsonValue(JEnumerable<JToken> jToken,string key)
IEnumerator enumerator = jToken.GetEnumerator();
while (enumerator.MoveNext())
JToken jc = (JToken)enumerator.Current;
if (jc is JObject||((JProperty)jc).Value is JObject)
return GetJsonValue(jc.Children(), key);
else
if (((JProperty)jc).Name == key)
return ((JProperty)jc).Value.ToString();
return null;
在调用的时候:
string jsonData = "\"name\":\"lily\",\"age\":23,\"addr\":\"city\":\"guangzhou\",\"province\":\"guangdong\"";
JObject jsonObj = JObject.Parse(jsonData);
Response.Write(GetJsonValue(jsonObj.Children(), "province"));
如果有多层嵌套的数组
string jsonData = "\"addr\":[\"city\":\"guangzhou\",\"province\":\"guangdong\",\"city\":\"guiyang\",\"province\":\"guizhou\"]";
JObject jsonObj = JObject.Parse(jsonData);
JArray jar = JArray.Parse(jsonObj["addr"].ToString());
JObject j = JObject.Parse(jar[0].ToString());
Response.Write(j["city"]);
JSON转XML
string xmlstr=((XmlDocument)JsonConvert.DeserializeXmlNode(jsonData)).InnerXml.ToString();本回答被提问者采纳
以上是关于应当把json怎么解析出来呢?的主要内容,如果未能解决你的问题,请参考以下文章
PHP 怎么解析远程URL 传过来json ,在前端显示出来
json的属性是对象,怎么用java解析该json字符串获得该对象属性呢
vue.js resource 请求后台得到json要怎么解析出来