小编Jos*_*sef的帖子

如何将JSON解析为int?

我已经解析了一些JSON数据,只要我将它存储在String变量中就可以正常工作.

我的问题是我需要一个int varibable中的ID而不是String中的ID.我试图做一个演员int id = (int) jsonObj.get("");

但是它给出了一条错误消息,我无法将对象转换为int.所以我尝试使用以下方式进行转换:

String id = (String) jsonObj.get("id");
int value = Integer.parseInt(id);
Run Code Online (Sandbox Code Playgroud)

但这也行不通.怎么了.JSON如何使用int?我的字符串工作得很好,只有当我尝试将它们作为int我才会遇到问题.

这是我的代码:

public void parseJsonData() throws ParseException {

        JSONParser parser = new JSONParser();
        Object obj = parser.parse(jsonData);
        JSONObject topObject = (JSONObject) obj;
        JSONObject locationList = (JSONObject) topObject.get("LocationList");
        JSONArray array = (JSONArray) locationList.get("StopLocation");
        Iterator<JSONObject> iterator = array.iterator();

        while (iterator.hasNext()) {

            JSONObject jsonObj = (JSONObject) iterator.next();
            String name  =(String) jsonObj.get("name");
            String id = (String) jsonObj.get("id");
            Planner.getPlanner().setLocationName(name);
            Planner.getPlanner().setArrayID(id);


        }

    }
Run Code Online (Sandbox Code Playgroud)

java json

18
推荐指数
3
解决办法
8万
查看次数

创建名为'entityManagerFactory的bean时出错

我正在尝试运行dbtest但是我收到以下错误:

"引起:org.springframework.beans.factory.BeanCreationException:在类路径资源[root-context.xml]中定义名称为'entityManagerFactory'的bean创建错误:init方法的调用失败;嵌套异常是java.lang.AbstractMethodError: org.springframework.orm.jpa.persistenceunit.SpringPersistenceUnitInfo.getValidationMode()Ljavax /持久/ ValidationMode;"

因此,我也得到以下错误:

"引起:org.springframework.beans.factory.BeanCreationException:在类路径资源[root-context.xml]中定义创建名为'personRepository'的bean时出错:bean的初始化失败;嵌套异常是org.springframework.beans.factory .BeanCreationException:在类路径资源[root-context.xml]中定义名称为'entityManagerFactory'的bean时出错:init方法的调用失败;嵌套异常是java.lang.AbstractMethodError:org.springframework.orm.jpa.persistenceunit.SpringPersistenceUnitInfo .getValidationMode()Ljavax /持久/ ValidationMode;

所以我们必须对我的root-context文件进行监视,它看起来如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- Root Context: defines shared resources visible to all other web components

     <bean id="personService" class="se.lowdin.civilforsvaret.webapp.services.PersonServiceImpl" />
     -->

     <bean id="personRepository" class="se.lowdin.civilforsvaret.webapp.repositories.PersonRepositoryJpa" />


    <!-- Database -->
        <bean id="datasource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost/civilforsvaret11" />
        <property name="username" value="bla" />
        <property name="password" value="bla" />
        </bean>

        <!-- Entity Manager -->
        <bean id="entityManagerFactory"
                class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
                <property name="dataSource" ref="datasource" />
                <property name="persistenceUnitName" value="civilforsvaret" />
        </bean> …
Run Code Online (Sandbox Code Playgroud)

java spring hibernate jpa

15
推荐指数
1
解决办法
10万
查看次数

标签 统计

java ×2

hibernate ×1

jpa ×1

json ×1

spring ×1