Convert JSON string to JSON object


String json = "{\"2\":\"efg\",\"1\":\"abc\"}"; JSONObject json_test = JSONObject.fromObject(json);

<dependency>
<groupId>
net.sf.json-lib </groupId>
<artifactId>
json-lib </artifactId>
<version>
2.4 </version>
<classifier>
jdk15 </classifier>
</dependency>

<!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
<dependency>
<groupId>
commons-beanutils </groupId>
<artifactId>
commons-beanutils </artifactId>
<version>
1.8.0 </version>
</dependency>

<!-- https://mvnrepository.com/artifact/commons-collections/commons-collections -->
<dependency>
<groupId>
commons-collections </groupId>
<artifactId>
commons-collections </artifactId>
<version>
3.2.1 </version>
</dependency>

<!-- https://mvnrepository.com/artifact/net.sf.ezmorph/ezmorph -->
<dependency>
<groupId>
net.sf.ezmorph </groupId>
<artifactId>
ezmorph </artifactId>
<version>
1.0.6 </version>
</dependency>

<!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
<dependency>
<groupId>
commons-lang </groupId>
<artifactId>
commons-lang </artifactId>
<version>
2.5 </version>
</dependency>



public String diliverMessage ( @RequestBody String message) throws Exception{

System. out .println(message) ;

try
{


if (message != null ) {

//完善用户画像
UserImage userImage = new UserImage() ;
//先获取cid bid组成主键

// net.sf.json.JSONObject json_test = net.sf.json.JSONObject.fromObject("{\"BID\":\"111\",\"CID\":\"222\",\"Content\":\"ok\",\"MsgTypeId\":\"1\"}");

net.sf.json.JSONObject json_test = net.sf.json.JSONObject. fromObject (message) ;

JSONObject jsonMessage =JSONObject. parseObject (json_test.toString()) ;

String CID = jsonMessage.get( "CID" ).toString() ;
String BID = jsonMessage.get( "BID" ).toString() ;
//主键生成方式cid+bid
userImage.setId(CID + BID) ;
userImage.setCid(CID) ;
Integer MsgTypeId = Integer. //Make judgment on getting message type msgTypeId message content Content
parseInt (jsonMessage.get( "MsgTypeId" ).toString()) ;
JSONObject content= null;
if
(MsgTypeId != 1 ){

content= JSONObject.parseObject ( jsonMessage.get ( "Content" ).toString()) ;

}


// So far UserImage only has id and cid properties and the rest are null

// text messages call the interface
if (MsgTypeId == 1 ) {
String content1=jsonMessage.get( "Content" ).toString() ;
//get the return The value is added and the fuse is added, so the result needs to be judged
HystrixUtils hystrixUtils= new HystrixUtils(content1) ;
//Get the result
String retValue1 = hystrixUtils.execute() ;

if
( "error" .equalsIgnoreCase(retValue1)){
//logger.error("failed",new Exception("The classification system failed to get the message").fillInStackTrace());
return " 0" ;
}
//System.out.println(retValue1+"...");

JSONObject dataObject=JSONObject.parseObject ( retValue1) ;

//The classification system gets the message successfully
if ( "ok" .equalsIgnoreCase(dataObject.get( " msg" ).toString())){

String retValue=dataObject.get( "data" ).toString() ;
JSONArray array = JSONObject. parseArray (retValue) ;
RetValue newValue = null;

for
( int i = 0 ; i < array.size() ; i++) {

//parse
newValue = JSONObject.parseObject ( array.get (i).toString( ) , RetValue.class ) ; / /Execute the operation analyzeRetValue(userImage , newValue) ; } } } else if (MsgTypeId == 20 ) { /* Get the template message about the phone number*/ //Get the phone number from Content String usermobile = content.get( "usermobile" ).toString() ;










analyzePhone(userImage , usermobile) ;
} else if (MsgTypeId == 22 ) {
/* is template message update model loan replacement local */

//get array
String demanditem = content.getString( "demanditem" ) ;
analyzeNotPhone(userImage , demanditem ) ;
}

}

return "1" ;

} catch (Exception e){
e.printStackTrace() ;
logger .error( "failed!" , e.fillInStackTrace()) ;
}

return "0" ;

}




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325904594&siteId=291194637