JSON到java对象转换的代码

时间:2014-02-19 10:35:30

标签: 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.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输入文件的一部分

{
            "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"
             },

                       ]
}               

2 个答案:

答案 0 :(得分:0)

据我所知,您的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"
        }
    ]
}

答案 1 :(得分:0)

使用两个ArrayLists verticesemployees

创建一个类
public class MyData {
    @SerializedName("Employee")
    private ArrayList<Employee> employees;
    @SerializedName("Vertices")
    private ArrayList<Vertices> vertices;

    //Setters and Getters
}

解析2个数组的代码。这将解析您的数组并将它们转换为Java ArrayLists。还可以通过在JSON Lint中粘贴文件内容来验证您的Json文件。

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);