Home > 类库下载 > java类库 > body text

Convert java string to json array and traverse

高洛峰
Release: 2016-10-09 16:26:21
Original
2079 people have browsed it

When you need to convert a string into a json array and traverse the contents.

First, import the two jar packages net.sf.json.JSONArray and net.sf.json.JSONObject

String str = "[{name:'a',value:'aa'},{name: 'b',value:'bb'},{name:'c',value:'cc'},{name:'d',value:'dd'}]" ; // An unconverted string

JSONArray json = JSONArray.fromObject(str ); // First convert the string into a JSONArray object

if(json.size()>0){
for(int i=0;i JSONObject job = json.getJSONObject(i); // Traverse the jsonarray array and convert each object into a json object
System.out.println(job.get("name")+"="); / / Get the attribute value in each object
}
}


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template