Java 解析JSON文件并迭代特定参数
我想检查第二个文件中缺少第一个文件的哪些元素 以下是第一种形式:Java 解析JSON文件并迭代特定参数,java,json,parsing,iteration,Java,Json,Parsing,Iteration,我想检查第二个文件中缺少第一个文件的哪些元素 以下是第一种形式: [ { "pId": "pId1", "Platform":["ios","and","web","winph","win"], "Name": "ay", "ShortDescription": "Mobi", "Detail" : { "IncentiveInformation": "ppp",
[
{
"pId": "pId1",
"Platform":["ios","and","web","winph","win"],
"Name": "ay",
"ShortDescription": "Mobi",
"Detail" : {
"IncentiveInformation": "ppp",
"DisplayName" : "vvv!",
"Description" : "mmm",
"TermsAndConditions": ".."
}
},
{
"pId": "pId2",
"Platform":["afasd","sdfsd","pppp","asdas","win"],
"Name": "ay",
"ShortDescription": "mob",
"PromotionDetail": {
"DebugMode": false,
"PromoDate": ["2015.01.01-00:01","2015.01.01-23:59"],
"IncentiveInformation": "PRO",
"Name": "iTunes",
"ShortDescription": "Punkte sammeln bei iTunes",
"DisplayName": null,
"Description": null,
"ImageURL": null,
"JumpToShopURL": "urlHere",
"JumpToShopName" : "Zu iTunes"
}
},
{
"pId": "pId3",
"Platform":["wqdsa"],
"Name": "poti",
"ShortDescription": "pun",
"ImageURL": "url.here",
"Promotion" : false,
"PromotionDetail": {
"DebugMode": false,
"PromoDate": ["2015.01.01-00:00","2015.01.01-23:59"],
"IncentiveInformation": "ppeur",
"Name": "namehere",
"ShortDescription": "tune",
"DisplayName": null,
"Description": null,
"ImageURL": null,
"JumpToShopURL": "noq",
"JumpToShopName" : "Zu"
}
}
]
这是第二个的形式:
{
"pList": [{
"shortName": "bb",
"longName": "bb",
"pId": "pId2",
"featured": true,
"pLog": "url.here",
"incentivation": "eu",
"details": {
"teaserImage": "image.url",
"description": "desc here",
"jumpToShopURL": "nurl",
"jumpToShopButton": "zubay",
"terms": [{
"headline": "Wichtig",
"body": "bodyline"
}]
}
}, {
"shortName": "one short name",
"longName": "bkp",
"pId": "pId1",
"featured": true,
"pLo": "some.pLo",
"incentivation": "1p",
"details": {
"teaserImage": "some.url",
"description": "desc",
"jumpToShopURL": "short url",
"jumpToShopButton": "Zuay",
"terms": [{
"headline": "Wichtig",
"body": "bodyhere"
}]
}
}]
}
我想把第一个的所有“pId”保存在一个列表(或数组)中,然后迭代该列表,检查新列表中是否存在pId。
所以我试过这个,但不起作用
有人能帮我吗?我试了一下,然后发现要将PID保存在列表或数组中有太多困难
有人有主意了吗
import java.io.*;
import org.json.*;
public class MainDriver {
public static void main(String[] args) throws JSONException {
String jsonData = readFile("C:\\Users\\kbelkhiria\\Desktop\\Karim_JSON\\alt.json");
JSONObject jobj = new JSONObject(jsonData);
JSONArray jarr = new JSONArray(jobj.getJSONArray("pList").toString());
for(int i = 0; i < jarr.length(); i++)
System.out.println("id: " + jarr.getString(i));
}
public static String readFile(String filename) {
String result = "";
try {
BufferedReader br = new BufferedReader(new FileReader(filename));
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
line = br.readLine();
}
result = sb.toString();
} catch(Exception e) {
e.printStackTrace();
}
return result;
}
}
import java.io.*;
导入org.json.*;
公共类主驱动程序{
公共静态void main(字符串[]args)抛出JSONException{
字符串jsonData=readFile(“C:\\Users\\kbelkhiria\\Desktop\\Karim\u JSON\\alt.JSON”);
JSONObject jobj=新的JSONObject(jsonData);
JSONArray jarr=新的JSONArray(jobj.getJSONArray(“pList”).toString();
for(int i=0;i
幸运的是,已经开发了一些库来解析任何JSON字符串,就像您提供的库一样。其中最受欢迎的是
使用此选项,您可以编写类似以下内容的代码:
import org.json.*;
String myString = ".." // The String representation you provided
JSONObject obj = new JSONObject(myString);
JSONArray arr = obj.getJSONArray("pList");
同一任务的另一个常用库是对于第二个表单,您有一个JSONObject,但它包含一些错误。请修复它们,或者再次使用第一个表单 解决方案 我在第二个文件中发现了一些错误,因此建议进行以下编辑:
- 将
更改为“jumpToShopURL”:nurl,
“jumpToShopURL”:null,
- 在“说明”的末尾添加逗号:“说明”
- 在“jumpToShopURL”的末尾添加逗号:“short url”
/*first file*/
String jsonData = readFile("C:\\Users\\kbelkhiria\\Desktop\\Karim_JSON\\alt.json");
JSONArray jarr = new JSONArray(jsonData);
/*array of first file's ids*/
ArrayList<String> srcArray = new ArrayList<>();
for(int i = 0; i < jarr.length(); i++) {
srcArray.add(jarr.getJSONObject(i).getString("pId"));
}
/*second file*/
// second form in a seperate file
JSONObject obj = new JSONObject(readFile("C:\\Users\\kbelkhiria\\Desktop\\Karim_JSON\\alt2.json"));
JSONArray arr = obj.getJSONArray("pList");
/*array of second file's ids*/
ArrayList<String> dstArray = new ArrayList<>();
for(int i = 0; i < arr.length(); i++) {
dstArray.add(jarr.getJSONObject(i).getString("pId"));
}
for (String string : srcArray) {
if (dstArray.indexOf(string)==-1)
System.out.println(string + " is missing in the second file");
}
/*第一个文件*/
字符串jsonData=readFile(“C:\\Users\\kbelkhiria\\Desktop\\Karim\u JSON\\alt.JSON”);
JSONArray jarr=新的JSONArray(jsonData);
/*第一个文件的ID数组*/
ArrayList srcArray=新的ArrayList();
for(int i=0;i
使用Jackson的一种可能解决方案如下:
private static final String JSON1 = // first json;
private static final String JSON2 = //second json;
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
List<LinkedHashMap> list1 = Arrays.asList(mapper.readValue(JSON1, LinkedHashMap[].class));
List<LinkedHashMap> list2 = Arrays.asList(mapper.readValue(mapper.readTree(JSON2).get("pList").toString(), LinkedHashMap[].class));
List<LinkedHashMap> missingItens = new ArrayList<>();
for (LinkedHashMap o1 : list1) {
if (!objectExistsInList(o1.get("pId").toString(), list2)) {
missingItens.add(o1);
}
}
}
private static boolean objectExistsInList(String pIdValue, List<LinkedHashMap> objs) {
for (LinkedHashMap map : objs) {
if (map.containsValue(pIdValue)) {
return true;
}
}
return false;
}
private静态最终字符串JSON1=//第一个json;
私有静态最终字符串JSON2=//第二个json;
公共静态void main(字符串[]args)引发IOException{
ObjectMapper mapper=新的ObjectMapper();
List list1=Arrays.asList(mapper.readValue(JSON1,LinkedHashMap[].class));
List list2=Arrays.asList(mapper.readValue(mapper.readTree(JSON2.get(“pList”).toString(),LinkedHashMap[].class));
List missinginitens=new ArrayList();
for(LinkedHashMap o1:list1){
如果(!objectExistsInList(o1.get(“pId”).toString(),list2)){
添加(o1);
}
}
}
私有静态布尔对象ExistSinList(字符串pIdValue,列表objs){
用于(LinkedHashMap映射:objs){
if(映射包含值(pIdValue)){
返回true;
}
}
返回false;
}
请记住,这是给定JSON的一个非常具体的实现。您如何知道元素是否在两个列表中?比较节点应该是什么?pId是唯一的。因此pId应该是比较节点。您可以告诉我它是什么异常吗?不幸的是,我目前无法编译自己,但它可能会给我一些简单的东西如果我对数组使用了错误的变量名,请检查我的编辑。这样上面的解决方案就可以完美地运行。