gpt4 book ai didi

java - 解析包含数组字段的 JSON 数组

转载 作者:行者123 更新时间:2023-12-01 23:11:04 25 4
gpt4 key购买 nike

我正在尝试存储从第三方 Web 服务获取的 JSON。问题是我得到了一个 JSON 数组。这些 JSON 中也有数组字段。我尝试使用 Jackson 和 Simple Json 解析 JSON,但我不能。请帮忙。我可能忽略了导致此问题的某些因素。

import java.io.BufferedReader;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.springframework.web.servlet.ModelAndView;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.Produces;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import org.json.JSONArray;
import org.json.JSONObject;
@Controller
public class RecommendationController {

@RequestMapping(value = "/getResult.do", method = RequestMethod.POST)
@Produces("application/json")
@ResponseBody
public ArrayList<Map<String, Object>> getResults(@RequestBody SearchQuery searchQuery) throws Exception{
ArrayList<Map<String, Object>> response = sendPost(searchQuery);
String email=null;
ArrayList<Map<String, Object>> finalResult = new ArrayList<Map<String, Object>>();
for (Map<String, Object> map : response) {
Map<String, Object> temp = new HashMap<String, Object>();
if(map.containsKey("TutorID"))
temp.put("TutorID", map.get("TutorID"));
if(map.containsKey("Name"))
temp.put("Name", map.get("Name"));
if(map.containsKey("City"))
temp.put("City", map.get("City"));
if(map.containsKey("State"))
temp.put("State", map.get("State"));
if(map.containsKey("TutorPictures"))
temp.put("TutorPictures", map.get("TutorPictures[0]"));
if(map.containsKey("FeePerHour"))
temp.put("FeePerHour", map.get("FeePerHour"));
if(map.containsKey("StarRatingAverage"))
temp.put("StarRatingAverage", map.get("StarRatingAverage"));
if(map.containsKey("TutorID") & map.containsKey("State") & map.containsKey("City"))
email = "http://localhost:8080/TestPrecision/contactTutor.do"+"?State="+map.get("State")+"&City="+((String) map.get("City")).replaceAll(" ", "-")+"&TutorID="+map.get("TutorID");
temp.put("Email", email);
finalResult.add(temp);
}
return finalResult;
}

private ArrayList<Map<String,Object>> sendPost(SearchQuery searchQuery) throws Exception {

ArrayList<Map<String,Object>> responseDataMap = null;
String url = "http://data.wyzant.com/api/search";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();

//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("Accept", "application/json");
String searchUrl = "";
searchUrl = "&SearchString="+searchQuery.getMessageString()+"&Zip="+searchQuery.getMessageZip()+"&MaxResults="+searchQuery.getMessageMaxResult();
String urlParameters = "Your_Partner_Key&"+searchUrl;

// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();

int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
/*StringBuilder responseStrBuilder = new StringBuilder();

String inputStr;
while ((inputStr = in.readLine()) != null)
responseStrBuilder.append(inputStr);
System.out.println(new JSONObject(responseStrBuilder.toString()));
*/ObjectMapper objectmapper = new ObjectMapper();
responseDataMap = objectmapper.readValue(in, new TypeReference<ArrayList<Map<String , Object>>>(){

});
System.out.println("JSON Map -----> /n"+responseDataMap);
in.close();
return responseDataMap;

}
}

下面是我从第三方 Web 服务获取的 JSON

[
{
"TutorID":8304029,
"Name":"Matthew B.",
"City":"Fremont",
"State":"CA",
"Zip":"94538",
"Title":null,
"FeePerHour":60,
"FreeResponse":"...",
"TravelDistance":15,
"TutorPictures":[
"http://dj1hlxw0wr920.cloudfront.net/userfiles/wyzfiles/be0c64e4-be09-4b38-bbd8- 72f3843cadde.jpg"
],
"ProfileLink":"http://www.wyzant.com/Tutors/CA/Fremont/8304029/? utm_source=partners\u0026utm_medium=api\u0026utm_term=1229\u0026utm_campaign=407\u0026g=3SEQ",
"EmailLink":"http://www.wyzant.com/Tutors/CA/Fremont/8304029/Contact.aspx?utm_source=partners\u0026utm_medium=api\u0026utm_term=1229\u0026utm_campaign=407\u0026g=3SEQ",
"Subjects":[
{
"Name":"American history",
"Description":null
},
{
"Name":"vocabulary",
"Description":null
},
{
"Name":"reading",
"Description":null
},
{
"Name":"writing",
"Description":null
},
{
"Name":"English",
"Description":null
}
],
"Reviews":null,
"StarRatingAverage":null,
"StarRatingCount":null,
"TutorRank":0.55803185701370239,
"College":null
},
{
"TutorID":8347645,
"Name":"Taner O.",
"City":"Fremont",
"State":"CA",
"Zip":"94538",
"Title":null,
"FeePerHour":65,
"FreeResponse":"...",
"TravelDistance":5,
"TutorPictures":[

],
"ProfileLink":"http://www.wyzant.com/Tutors/CA/Fremont/8347645/?utm_source=partners\u0026utm_medium=api\u0026utm_term=1229\u0026utm_campaign=407\u0026g=3SEQ",
"EmailLink":"http://www.wyzant.com/Tutors/CA/Fremont/8347645/Contact.aspx?utm_source=partners\u0026utm_medium=api\u0026utm_term=1229\u0026utm_campaign=407\u0026g=3SEQ",
"Subjects":[
{
"Name":"calculus",
"Description":null
},
{
"Name":"chemistry",
"Description":null
},
{
"Name":"geometry",
"Description":null
},
{
"Name":"physics",
"Description":null
},
{
"Name":"SAT math",
"Description":null
}
],
"Reviews":null,
"StarRatingAverage":null,
"StarRatingCount":null,
"TutorRank":-0.39533111453056335,
"College":null
}

我打算从此 JSON 获取导师图像、导师姓名、主题、FeeperHour、StarRating。我已经成功获取了导师姓名、FeesperHour 和 StarRatings,但无法检索导师图像和主题,因为它们是数组,而且我无法解析它。请帮忙。下面是我设法获取的 JSON。

[
{
"Name":"Courtney D.",
"FeePerHour":60,
"State":"NY",
"TutorPictures":null,
"StarRatingAverage":null,
"TutorID":7874994,
"City":"Milford"
}
]

最佳答案

如果您不想使用类并反序列化为类,请不要尝试反序列化为 Java 类型,而是反序列化为 JsonNode:

final JsonNode data = mapper.readTree(yourURLObject);

请参阅 JsonNode 的 javadoc。您可以使用它轻松浏览 JSON。

例如,以下是如何从每个数组元素打印出 TutorID 对象成员:

// This cycles through a JSON array's elements or a JSON Object's member values;
// empty iterator for all other JSON value types
for (final JsonNode element: data)
System.out.println(data.path("TutorID"));

关于java - 解析包含数组字段的 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21967406/

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