gpt4 book ai didi

Java-如何将解析嵌套的json数据存储到Java列表中

转载 作者:行者123 更新时间:2023-12-04 08:45:03 26 4
gpt4 key购买 nike

我试图在 google api 的帮助下每天监视我的网站性能
我试图获取 项目 在来自 pagespeedonline 的 googleapi 的网络请求中
但它不适用于我的代码
REST API 链接:
https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=https://lifemachi.blogspot.com&key=AIzaSyBfHVlhNEnf26Ea8-ZOhiYOe0HrQZtLvRI&category=performance&strategy=desktop
我特别想得到
lighthouseResult -> 审计 -> 网络请求 -> 详细信息 -> 项目
并将每个项目存储到记录中...
我试过下面的代码


package FirstTestNgPackage;



import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import org.json.*;

public class testingJSON {
static String inline = "";
public static void main(String args[]) throws JSONException, InterruptedException, IOException {
// url
URL url = new URL("https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=https://lifemachi.blogspot.com&key=AIzaSyBfHVlhNEnf26Ea8-ZOhiYOe0HrQZtLvRI&category=performance&strategy=desktop");
// read it from URL
Scanner sc = new Scanner(url.openStream()); Thread.sleep(300);
String jsonDataString = sc.nextLine();
while(sc.hasNext())
{
inline+=sc.nextLine();
}
sc.close();

List<String> list = new ArrayList<String>();


// just print that inline var

System.out.println(inline);
System.out.println("--------1");

}
}
我得到了正确的输出......
但是如何在列表中存储项目值?
提前致谢

最佳答案

首先,您必须将名为“inline”的输出数组解析为有效的 JSON string 。
在这里您可以使用 的功能org.json 图书馆,

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import org.json.*;

public class testingJSON {

static String inline = "";

public static void main(String args[]) throws JSONException, InterruptedException, IOException {

// url
URL url = new URL("https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=https://lifemachi.blogspot.com&key=AIzaSyBfHVlhNEnf26Ea8-ZOhiYOe0HrQZtLvRI&category=performance&strategy=desktop");

// read it from URL

Scanner sc = new Scanner(url.openStream()); Thread.sleep(300);
String jsonDataString = sc.nextLine();

while(sc.hasNext()){
inline+=sc.nextLine();
}

sc.close();

// just print that inline var
System.out.println(inline);
System.out.println("--------1");

//Tokenize the string data json array
JSONArray data = new JSONArray(new JSONObject(new JSONTokener(inline)));

//or JSONArray data = new JSONArray(new JSONObject(inline));

//The array list we want to insert the formatted JSON string
ArrayList<String> list = new ArrayList<String>();

if(data !=null){
for(int i=0;i<data.length();i++){
list.add(data.getString(i));
}
}

System.out.println(list);
}
}
在这里我有以下错误
Exception in thread "main" org.json.JSONException: A JSONObject text must begin with '{' at 3 [character 4 line 1]
at org.json.JSONTokener.syntaxError(JSONTokener.java:432)
at org.json.JSONObject.<init>(JSONObject.java:184)
at testingJson.main(testingJson.java:31)
从中我们可以识别出一些东西 遗漏格式 内联 变量同时将其标记为 JSON 字符串
在 JSON 字符串中,格式必须是这样的 [ { JSON 数据 } ] .

关于Java-如何将解析嵌套的json数据存储到Java列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64366803/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com