Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JSON到java对象转换的代码_Java_Json_Gson - Fatal编程技术网

JSON到java对象转换的代码

JSON到java对象转换的代码,java,json,gson,Java,Json,Gson,我们有一个包含2个数组的JSON文件。我们需要将其转换为java对象。我们使用GSON和JSON进行了尝试。但是我们只能转换第一个数组。请建议我们使用一个简单的代码将这两个数组转换为java对象。 这是我们尝试的代码 import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOExcepti

我们有一个包含2个数组的JSON文件。我们需要将其转换为java对象。我们使用GSON和JSON进行了尝试。但是我们只能转换第一个数组。请建议我们使用一个简单的代码将这两个数组转换为java对象。 这是我们尝试的代码

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import org.json.simple.JSONArray;
//import java.util.Scanner;
import com.google.gson.reflect.*; 
import com.google.gson.Gson;

public class demo {   

    //private Object json;

    public void jsonconversion(){   
        Employee empl =null;
        Vertices verti=null;
        BufferedReader reader = null;
            try {    

                {
                    // obtained a file object from json file   
                    reader = new BufferedReader(new FileReader(new File("H:\\my work\\project\\src\\com\\inautix\\dggeneration\\example.json")));
                    Map<String, List<Employee>> object = (new Gson()).fromJson(reader, new TypeToken<Map<String, List<Employee>>>(){}.getType());
                    List<Employee> data=new ArrayList<Employee>();
                    if (object.values().iterator().hasNext()){
                        data = object.values().iterator().next();
                  // this is you data in your case 3 FoodItemData entries
                    }
                    for(int i=0;i<(data.size()-1);i++){
                        empl=data.get(i);
                        empl.printDetails(empl);
                    } 
                    Map<String, List<Vertices>> objectver = (new Gson()).fromJson(reader, new TypeToken<Map<String, List<Vertices>>>(){}.getType());
                    List<Vertices> dataver=new ArrayList<Vertices>();
                    if (objectver.values().iterator().hasNext()){
                        dataver = objectver.values().iterator().next();
                        //  this is you data in your case 3 FoodItemData entries
                    }
                    for(int i=0;i<(dataver.size()-1);i++){
                        verti=dataver.get(i);
                        verti.printDetails(verti);
                    } 

                }}
            catch (FileNotFoundException e) {   
                System.out.println("inside catch in demo");
                e.printStackTrace();

            } 
            finally{
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        System.out.print(e.getMessage());
                    }
                    }
                }

                } 
        }   
//package com.beingjavaguys.core;   

据我所知,你的JSON是无效的。我还没有测试过您的代码,但您确实应该在尝试其他任何操作之前在有效的JSON上测试它

下面是上述示例的有效版本

{
    "Employee": [
        {
            "Name": "abc",
            "EmpId": "123",
            "Team": "Trainees",
            "Picture": "0x3523452345"
        },
        {
            "Name": "Pqr",
            "EmpId": "122",
            "Team": "Trainees",
            "Picture": "0x3523452345"
        },
        {
            "Name": "xyz",
            "EmpId": "134",
            "Team": "Trainees",
            "Picture": "0x3523445634"
        }
    ],
    "Vertices": [
        {
            "Project": "java",
            "_id": "101",
            "_type": "edge",
            "_outV": "123",
            "_inV": "333",
            "_label": "Member_of"
        },
        {
            "_id": 102,
            "_type": "edge",
            "_outV": "123",
            "_inV": "122",
            "_label": "Friend_of"
        },
        {
            "Project": "php",
            "_id": 103,
            "_type": "edge",
            "_outV": "222",
            "_inV": 333,
            "_label": "Member_of"
        }
    ]
}

创建一个包含两个ArrayList
顶点的类
雇员

public class MyData {
    @SerializedName("Employee")
    private ArrayList<Employee> employees;
    @SerializedName("Vertices")
    private ArrayList<Vertices> vertices;

    //Setters and Getters
}

使用gson通常可以避免这种单调乏味的代码。你能通过添加一个JSON示例来澄清你的问题吗?看看Jackson或JSON Simple。这不是有效的JSON(我至少看到一个“}”)。假设您最初尝试使用GSON时输入的是有效的。@user3321815请在下面找到我的答案。。
public class MyData {
    @SerializedName("Employee")
    private ArrayList<Employee> employees;
    @SerializedName("Vertices")
    private ArrayList<Vertices> vertices;

    //Setters and Getters
}
BufferedReader reader = new BufferedReader(new FileReader(new File("H:\\my work\\project\\src\\com\\inautix\\dggeneration\\example.json")));
MyData data = (new Gson()).fromJson(reader, MyData.class);