Home  >  Article  >  类库下载  >  Convert java string to json array and traverse

Convert java string to json array and traverse

高洛峰
高洛峰Original
2016-10-09 16:26:212056browse

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
}
}


Statement:
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