java解析json,The constructor JSONObject(String) is undefined,错哪了啊??

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java解析json,The constructor JSONObject(String) is undefined,错哪了啊??相关的知识,希望对你有一定的参考价值。

///json-lib-2.4-jdk15.jar已导入
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

public class Test

public static void main(String[] args) throws JSONException
String jsonString = "\"people\":[\"firstName\":\"Brett\",\"lastName\":\"McLaughlin\",\"email\":\"aaaa\",\"firstName\":\"Jason\",\"lastName\":\"Hunter\",\"email\":\"bbbb\",\"firstName\":\"Elliotte\",\"lastName\":\"Harold\",\"email\":\"cccc\"]";
JSONObject dataJson = new JSONObject(jsonString);
JSONArray array = dataJson.getJSONArray("people");

for (int i = 0; i < array.length(); i++)
System.out.println("array:" + array.get(i));
System.out.println("firstName:" + array.getJSONObject(i).get("firstName"));
System.out.println("lastName:" + array.getJSONObject(i).get("lastName"));
System.out.println("email:" + array.getJSONObject(i).get("email"));



import java.util.Iterator;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Test6

public static void main(String[] args)

String str =
        "\\"people\\":[\\"firstName\\":\\"Brett\\",\\"lastName\\":\\"McLaughlin\\",\\"email\\":\\"aaaa\\",\\"firstName\\":\\"Jason\\",\\"lastName\\":\\"Hunter\\",\\"email\\":\\"bbbb\\",\\"firstName\\":\\"Elliotte\\",\\"lastName\\":\\"Harold\\",\\"email\\":\\"cccc\\"]";
JSONObject jo = JSONObject.fromString(str);
JSONArray info = jo.getJSONArray("people");
System.out.println(info);
for(int i = 0; i < info.length(); i++)

JSONObject obj = info.getJSONObject(i);
Iterator it = obj.keys();
while(it.hasNext())

String key = (String) it.next();
System.out.println(obj.get(key));



参考技术A net.sf.json.JSONObject这个类里面没有JSONObject(String)的初始化方法
所以不能那样初始化JSONObject实例

以上是关于java解析json,The constructor JSONObject(String) is undefined,错哪了啊??的主要内容,如果未能解决你的问题,请参考以下文章

jQuery解析JSON出现SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data(示例代码

JSON解析时出现:TypeError: the JSON object must be str, bytes or bytearray, not NoneType

net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案

[转]快速搞懂Gson的用法

typescript TypeScript Private Constructos和Singletons

在颤振中解析json