简体   繁体   English

将String转换为JSON对象数组并解析它们

[英]Converting a String to array of JSON objects and parsing those

I have a string in the format like this 我有一个像这样的格式的字符串

[{name:"abc",test:"123",jack:{tret:"abc",cold:"yes"},killer:"yes"},{name:"pqr",test:"456",jack:{tret:"hg",cold:"No"},killer:"No"}]

I know that this is an array of JSON objects.How do I convert this so? 我知道这是一个JSON对象数组。如何转换呢?

As of now i am just using split and removing all the commas and other things from the string and storing in an array.Is there any better way to do so? 截至目前我只是使用拆分并从字符串中删除所有逗号和其他东西并存储在数组中。有没有更好的方法呢?

Use the https://code.google.com/p/json-simple/ library for handling JSON. 使用https://code.google.com/p/json-simple/库来处理JSON。

Here is a sample code: 这是一个示例代码:

JSONObject json = (JSONObject)new JSONParser().parse("{\"name\":\"MyNode\", \"width\":200, \"height\":100}");
System.out.println("name=" + json.get("name"));
System.out.println("width=" + json.get("width"));

Please refer to this stackoverflow answer for more answers in this matter. 有关此问题的更多答案,请参阅此stackoverflow答案

Using a JSON library is definitely the way to go, but I might suggest one that's being actively developed such as jackson or gson . 使用JSON库肯定是要走的路,但我可能会建议一个正在积极开发的,如jacksongson Both have extensive documentation and examples. 两者都有大量文档和示例。

我使用谷歌的Gson

JsonElement parse = (new JsonParser()).parse(jsonString);

你可以使用杰克逊库,你可以通过它来解析http://www.mkyong.com/java/jackson-tree-model-example/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM