【问题标题】:JSON file parsing to database in Java web program在 Java Web 程序中将 JSON 文件解析为数据库
【发布时间】:2012-01-18 09:57:20
【问题描述】:

我有一个如下的大 JSON 文件。

[{"id":0,"name":"Mrs. Tess Goyette","priceRange":{"min":86,"max":87},"requirements":["close to transport","internet","bath","gym"]},..

同样有 999 个这样的项目。我想将此数据发送到 MySQL 数据库。还 用它做一些交易。还有一个像这样的文件,其中包含 40 个酒店名称和称为住宿的数据。

我无法使用 java 解析此文件。有人可以帮忙吗?我尝试在其中创建一个 JSON 对象和数组,但失败了。enter code here

感谢任何帮助。

是的,我确实尝试过编写这样的代码。

public static void main(String[] args) {


        JSONParser parser = new JSONParser();
        try {

                        JSONParser parser = new JSONParser();

            InputStream is = hoteldemo.class.getResourceAsStream("accommodation.json");


            String jsonTxt = IOUtils.toString(is); 
            JSON json = JSONSerializer.toJSON(jsonTxt);

            JSONObject json1 = (JSONObject)JSONSerializer.toJSON(jsonTxt); 
            JSONObject json2= json1.getJSONObject("menu");

              double id = json2.getDouble("id");
            String name = json2.getString("name");
            int price=json2.getInt("price");

            JSONArray jarray=json2.getJSONArray("attributes");
            //JSONArray jarray1=json1.getJSONArray("capacity");

           int a= json2.size();
            for(int i=0;i<json2.size();i++)
            {
            System.out.println("The id is "+ json2.getDouble("id"));
            System.out.println("The name is "+ json2.getString("name"));
            System.out.println("The price is "+ json2.getInt("price"));
         System.out.println("the size of object is" + a);

            }
         /*   for(int i=0;i<jarray.size();i++)
            {
                System.out.println("This is ameneities id is " + jarray.getString(i));
            }
            for(int j=0;j<jarray1.size();j++)
            {
                System.out.println("This is ameneities id is " + jarray1.getString(j));

            }*/


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    }

但它只给了我一个对象,比如只有第一个 id 和下一个 id 在 for 循环中不可访问。

【问题讨论】:

  • 请向我们展示您的代码并告诉我们what you have already tried
  • 我做过这样的事情。
  • “我做过这样的事情。” 我将其提名为当天最无意义(因此也毫无意义)的评论。 1)准确地告诉我们,而不是“类似”你尝试了什么。 2)“这个”什么?当我们问您尝试了什么时,我们的意思是“什么代码?”。虽然“this”是 Java 关键字,但它不是代码。
  • 我猜,他在他的问题中添加了代码。但是是的,评论确实具有欺骗性;)
  • 您使用哪个库来处理 JSON? Google GSON, json.org?

标签: java json html-parsing


【解决方案1】:

在处理 JSON 格式的数据时,我通常喜欢做的第一件事就是不处理 JSON。我经常喜欢填充一些好的旧 POJO,然后继续处理任何业务逻辑。要将 JSON 映射到 Java 数据结构,我的首选是使用 Jackson。以下是使用 Jackson 反序列化原始问题中的示例 JSON 的示例。

import java.io.File;
import java.util.List;

import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;
import org.codehaus.jackson.annotate.JsonMethod;
import org.codehaus.jackson.map.ObjectMapper;

public class JacksonFoo
{
  public static void main(String[] args) throws Exception
  {
    ObjectMapper mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY);

    List<AccomodationRequest> requests = mapper.readValue(new File("accommodation.json"), mapper.getTypeFactory().constructCollectionType(List.class, AccomodationRequest.class));
    System.out.println(requests);

    for (AccomodationRequest request : requests)
    {
      System.out.println("----");
      System.out.println("id is " + request.id);
      System.out.println("name is " + request.name);
    }
  }
}

class AccomodationRequest
{
  int id;
  String name;
  PriceRange priceRange;
  List<String> requirements;

  @Override
  public String toString()
  {
    return String.format("AccomodationRequest: id=%d, name=%s, priceRange=%s, requirements=%s", id, name, priceRange, requirements);
  }
}

class PriceRange
{
  int min;
  int max;

  @Override
  public String toString()
  {
    return String.format("PriceRange: min=%d, max=%d", min, max);
  }
}

【讨论】:

  • 您将哪个库用于 objectmapper 类。 eclipse 没有为 1.4.2 jar 文件检测到该类
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-24
  • 2020-02-26
  • 2016-04-05
  • 2015-03-09
  • 2013-12-09
  • 1970-01-01
相关资源
最近更新 更多