嗨我在Json请求中有一个字段,它的值是Json。所以我将字符串转换为Json应用转义字符。这个转换后的字符串再次通过一个Object to Json转换器,转换它是一个无效的Json。
我需要什么:
"attributes" : {"type" : "CARES_Diagnosis_Survey__c", "referenceId" : "ref1"},
我在做什么:
public static final String ATTRIBUTES_BEGIN = "{\"type\"" +":"+ "\"CARES_Diagnosis_Survey__c\""+","+"\"referenceId\"" +":"+ "\"ref";
public static final String ATTRIBUTES_END = "\"}";
String attributes = ServiceConstants.ATTRIBUTES_BEGIN + ServiceConstants.ATTRIBUTES_END;
salesForceDiagnosisObject.setAttributes(attributes);
This is salesForceDiagnosisObject is going through Object to Json transformer in spring integration
<!-- turn to json -->
<int:object-to-json-transformer id="sfdcDiagnosisOutboundToJsonTransformer"
input-channel="sfdcDiagnosisObjectToJsonConverter"
output-channel="sfdcDiagnosisOutboundToJson"
/>
我得到的是什么:
"attributes":"{\"type\":\"CARES_Diagnosis_Survey__c\",\"referenceId\":\"ref\"}"
我想得到什么:
"attributes" : {"type" : "CARES_Diagnosis_Survey__c", "referenceId" : "ref1"}
我曾尝试在此字段上使用JSonIgnore来跳过序列化,但如果我这样做,则完全省略了该字段。 请帮帮我。
答案 0 :(得分:2)
您可能以错误的方式执行操作,不将字符串映射到JSON,将Object映射到JSON。
因此,如果您希望将salesForceDiagnosisObject序列化为包含如下属性的JSON:
<?php
$d=$_POST['d']; $zipcode=$_POST['zipcode'];
//* -------- CONNECT TO MY DATABASE --------- *//
$sql2="SELECT * FROM zips WHERE zipcode='$zipcode'";
$result2=mysql_query($sql2); $count=mysql_num_rows($result2);
if($count==1) {
while($row = mysql_fetch_array($result2)) {
$lat1 = $row['Latitude']; $lon1 = $row['Longitude'];
}
}
$r = 3959;
$latN = rad2deg(asin(sin(deg2rad($lat1)) * cos($d / $r) +
cos(deg2rad($lat1)) * sin($d / $r) * cos(deg2rad(0))));
$latS = rad2deg(asin(sin(deg2rad($lat1)) * cos($d / $r) +
cos(deg2rad($lat1)) * sin($d / $r) * cos(deg2rad(180))));
$lonE = rad2deg(deg2rad($lon1) +
atan2(sin(deg2rad(90)) * sin($d / $r) * cos(deg2rad($lat1)), cos($d / $r) -
sin(deg2rad($lat1)) * sin(deg2rad($latN))));
$lonW = rad2deg(deg2rad($lon1) +
atan2(sin(deg2rad(270)) * sin($d / $r) * cos(deg2rad($lat1)), cos($d / $r)
sin(deg2rad($lat1)) * sin(deg2rad($latN))));
$adjacents = 2;
$query = "SELECT COUNT(*) as num FROM zips WHERE (Latitude <= $latN
AND Latitude >= $latS AND Longitude <= $lonE AND Longitude >= $lonW)
AND city != ''";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];
$targetpage = "search.php";
$limit = 10;
$page = $_GET['page'];
if($page)
$start = ($page - 1) * $limit;
else
$start = 0;
$query3 = "SELECT * FROM zips WHERE (Latitude <= $latN AND Latitude
>= $latS AND Longitude <= $lonE AND Longitude >= $lonW) AND city != ''
ORDER BY state, city, Latitude, Longitude LIMIT $start, $limit";
$result3 = mysql_query($query3);
if ($page == 0)
$page = 1;
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$lpm1 = $lastpage - 1;
$pagination = "";
if($lastpage > 1) {
$pagination.= "<div
class=\"pagination\">"; //previous button if ($page > 1)
$pagination.= "<a href=\"$targetpage?page=$prev\">« previous</a>";
else $pagination.= "<span class=\"disabled\">« previous</span>";
//pages if ($lastpage < 7 + ($adjacents * 2)) //not enough pages
to bother breaking it up { for ($counter = 1; $counter <=
$lastpage; $counter++) {
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
} }
elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
{ //close to beginning; only hide later pages if($page < 1 +
($adjacents * 2)) {
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
} //in
middle; hide some front and some back elseif($lastpage -
($adjacents * 2) > $page && $page > ($adjacents * 2)) {
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
$pagination.= "...";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents;
$counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>"; } //close
to end; only hide early pages else { $pagination.= "<a
href=\"$targetpage?page=1\">1</a>"; $pagination.= "<a
href=\"$targetpage?page=2\">2</a>";
$pagination.= "...";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <=
$lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
} } }
//next button if ($page < $counter - 1) $pagination.= "<a
href=\"$targetpage?page=$next\">next »</a>"; else $pagination.=
"<span class=\"disabled\">next »</span>"; $pagination.= "</div>\n";
} ?>
<?php echo "<table align='center', border='1',table width='90%',
cellpadding='0', cellspacing='0' class='zui-table
zui-table-rounded'>";
echo "<tr>";
echo "<td>" . "<font color='black', face='verdana',
size='2'><b>City</b></font></td>"; echo "<td>" . "<font
color='black', face='verdana', size='2'><b>State</b></font></td>";
echo "<td>" . "<font color='black', face='verdana', size='2'><b>Zip
Code</b></font></td>";
echo "</tr>";
while($row = mysql_fetch_array($result3)) {
echo "<tr>";
echo "<td align='left'><font color='black', face='verdana',
size='2'>" . $row['city'] . "</font></td>";
echo "<td align='left'><font color='black', face='verdana',
size='2'>" . $row['state'] . "</font></td>";
echo "<td align='left'><font color='black', face='verdana',
size='2'>" . $row['zipcode'] . "</font></td>";
echo "</tr>\n";
}
echo "</table>\n<br />\n";
?>
<br>
<?=$pagination?> </html>
您的salesForceDiagnosisObject类不能:
{
"key1" : "value1",
"key2" : "value2",
....
"attributes" : {
"type" : "CARES_Diagnosis_Survey__c",
"referenceId" : "ref1"
}
必须是这样的:
class SalesForceDiagnosisObject {
String key1;
String key2;
String attributes;
....
}
,你的属性应该像这样设置:
class SalesForceDiagnosisObject {
String key1;
String key2;
DiagnosisAttribute attributes;
....
}
答案 1 :(得分:1)
你的String看起来是正确的,它只是 反向斜杠(用于转义双引号),使字符串看起来有点奇怪。但是,如果您sysout
生成的字符串,则不包括斜杠。
以下是使用attributes
将Map
转换为ObjectMapper
的示例代码:
public static void main(String[] args) throws Exception{
String ATTRIBUTES_BEGIN = "{\"type\"" +":"+ "\"CARES_Diagnosis_Survey__c\""+","+"\"referenceId\"" +":"+ "\"ref";
String ATTRIBUTES_END = "\"}";
String attributes = ATTRIBUTES_BEGIN + ATTRIBUTES_END;
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> value = mapper.readValue(attributes, new TypeReference<Map<String,Object>>() {});
System.out.println(value);
}