1. c#中怎么解析多层json数据
在.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转XMLstring xmlstr=((XmlDocument)JsonConvert.DeserializeXmlNode(jsonData)).InnerXml.ToString();
2. java如何解析多层json
1. 使用标准的Json对象,如org.json.JSONObject json = new org.json.JSONObject(yourJsonString);然后通过get(keyString)逐个解析2. 构造一个类MyObject,根据你提出的问题,其中添加 String name,List<MyObject child, 两个属性,增加getter setter 方法,通过com.google.gson.Gson进行解析。步骤如下:String jsonStr = "{'name':'111','child':[{'name':'222','child':[{'name':'333'}]},{'name':'2221'}]}";Gson gson = new Gson();MyObject obj = gson.fromJson(jsonStr,MyObject.class);import java.util.List;import com.google.gson.Gson;public class MyObject {String name;List<MyObject child;public String getName() {return name;}public void setName(String name) {this.name = name;}public List<MyObject getChild() {return child;}public void setChild(List<MyObject child) {this.child = child;}public static void main(String[] args) {String jsonStr = "{'name':'111','child':[{'name':'222','child':[{'name':'333'}]},{'name':'2221'}]}";Gson gson = new Gson();MyObject obj = gson.fromJson(jsonStr,MyObject.class);System.out.println(obj.getChild().get(0).getName()); // –222System.out.println(obj.getChild().get(1).getName()); // –222
3. 用Gson怎么解析多层的json数据啊
StringjsonStr="[{'id':'11','parentId':'root','refObj':{'existType':'exist','deptType':'emp','treeNodeType':'dept'}}]"; Gsongson=newGson(); Typetype=newTypeToken<List<Bean>>(){ }.getType(); List<Bean>list=gson.fromJson(jsonStr,type);
4. 如何用Python解析多层嵌套的JSON
单纯从你给的这个链接,使用requests.get(url).json()这样拿到的数据,是dict类型,取值正常…..
5. 用java怎么解析复杂的JSON
json-lib-2.4-jdk15.jar 这个包还另需一下几个包。jakarta commons-lang 2.5jakarta commons-beanutils 1.8.0jakarta commons-collections 3.2.1jakarta commons-logging 1.1.1ezmorph 1.0.6把上边的json定义为一个字符串 str,建议不要直接用还是自己动手写写。这样比较容易掌握方法。JSONObject ob=JSONObject.fromObject(str);Object success=ob.get("success");Object errorMsg=ob.get("errorMsg");System.out.println(success);System.out.println(errorMsg);JSONObject data=ob.getJSONObject("data"); Object total=data.get("total");System.out.println(total);JSONArray array=data.getJSONArray("rows");JSONObject rows=null;for(int i=0;i<array.size();i++){rows=array.getJSONObject(i);Object id=rows.get("id");System.out.println(id);Object workName=rows.get("workName");System.out.println(workName);Object assigneeName=rows.get("assigneeName");System.out.println(assigneeName);Object name=rows.get("name");System.out.println(name);Object processInstanceInitiatorName=rows.get("processInstanceInitiatorName");System.out.println(processInstanceInitiatorName);Object processInstanceStartTime=rows.get("processInstanceStartTime");System.out.println(processInstanceStartTime);Object createTime=rows.get("createTime");System.out.println(createTime);Object eDate=rows.get("eDate");System.out.println(eDate); }
6. json多层集合嵌套解析求助
首先楼上说的部分是对的。那么我先假设你已经获得了这个字符串,将之定义为jsonString.JSONObject jsonObject = new JSONObject(jsonString);然后,根据嵌套的种类,可以确定下一步如何取值。根据你的情况来看。应该是:String user_get_response = jsonObject.getString("user_get_response");JSONObject user_get_response_obj = new JSONObject(user_get_response);这是针对第一层嵌套的。一层一层的取下去就好。如果是数组类型的,就用JSONArray来接收。JSONArray jsonArray = new JSONArray(jsonObject.getString("xxx"));总之就是由外到内去解析就好
7. Java json多层解析
{}是Object []是Array一层一层解析的话,{}对应JSONObject,[]对应JSONArray
8. 多层的Json怎么解析
主要看你需要json里面的什么内容了,可以转成map或是list类型,然后逐层去解析,转map:JSON.parseObject(str);list:(list)JSONArray.fromObject(json)
9. 多层json数据怎么使用 fastjson 解析
二、解析制Json:(1)JavaBean在CODE上查看代码片派生到我的代码片Class class= JSON.parseObject(jsonString, Class.class); (2)List<JavaBean>在CODE上查看代码片派生到我的代码片List<Class> class=JSON.parseArray((jsonString, Class.class); (3)List<String>在CODE上查看代码片派生到我的代码片List<String> listString = JSON.parseArray(jsonString, String.class); (4)List<Map<String,Object>>在CODE上查看代码片派生到我的代码片List<Map<String, Object>> listMap = JSON.parseObject(jsonString, new TypeReference<List<Map<String,Object>>>(){});