从字符串转换为 JSONArray
Converting from a String to JSONArray
我有这个 JSONArray data
,其中只有一个对象。此对象中的 prop
元素包含一个类似于 Json 响应的字符串,如下所示。我如何遍历所有可能产生 JSONArray 的字符串?
我知道我得到的响应不是数组,但我无法控制它。
"data": [
{
"prop": "{\"Name\":\"StockoutTransferDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"LoadBearing\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCBOOLEAN\",\"value\":true},\"Unit\":\"$\"},{\"Name\":\"StockoutDeliveryDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"Status\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"OnTransport\"},\"Unit\":\"$\"},{\"Name\":\"Tracked\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"true\"},\"Unit\":\"$\"},{\"Name\":\"StockoutReturnDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"IsExternal\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCBOOLEAN\",\"value\":false},\"Unit\":\"$\"},{\"Name\":\"PlannedStockoutDeliveryDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"10-03-2019\"},\"Unit\":\"$\"},{\"Name\":\"StockinReturnLocation\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"PlannedAssemblyDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"27-04-2019\"},\"Unit\":\"$\"},{\"Name\":\"StockoutTransferLocation\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"StockinReturnDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"FabricatedLocation\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"StockinArrivalLocation\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"StockinTransferDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"Reference\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCIDENTIFIER\",\"value\":\"300 x 350\"},\"Unit\":\"$\"},{\"Name\":\"PlannedStockinArrivalDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"11-03-2019\"},\"Unit\":\"$\"},{\"Name\":\"PlannedFabricatedDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"01-03-2019\"},\"Unit\":\"$\"},{\"Name\":\"AssemblyDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"Slope\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCPLANEANGLEMEASURE\",\"value\":\"0\",\"orig_value\":\"0.\"},\"Unit\":\"$\"},{\"Name\":\"FabricatedDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"StockoutReturnLocation\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"Span\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCPOSITIVELENGTHMEASURE\",\"value\":\"4750\",\"orig_value\":\"4750.\"},\"Unit\":\"$\"},{\"Name\":\"StockinArrivalDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"StockoutDeliveryLocation\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"Status\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"Assembled\"},\"Unit\":\"$\"}"
}
],
我的代码片段是:
JSONArray jsonArray = response.getJSONArray("data");
JSONObject data = jsonArray.getJSONObject(0);
String prop = data.getString("prop");
JSONArray jsonArray1 = new JSONArray(prop);
我遇到错误 org.json.JSONException: Value {"Name":"StockTransferLocation","Description":"$","NominalValue":{"type":"IFCTEXT","value":"East"},"Unit":"$"} of type org.json.JSONObject cannot be converted to JSONArray
提前致谢!
只需要在String的开头和结尾加上一个方括号,然后尝试将其转换为JSONArray。
"data": [
{
"prop": "[{\"Name\":\"StockoutTransferDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"LoadBearing\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCBOOLEAN\",\"value\":true},\"Unit\":\"$\"},{\"Name\":\"StockoutDeliveryDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"Status\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"OnTransport\"},\"Unit\":\"$\"},{\"Name\":\"Tracked\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"true\"},\"Unit\":\"$\"},{\"Name\":\"StockoutReturnDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"IsExternal\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCBOOLEAN\",\"value\":false},\"Unit\":\"$\"},{\"Name\":\"PlannedStockoutDeliveryDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"10-03-2019\"},\"Unit\":\"$\"},{\"Name\":\"StockinReturnLocation\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"PlannedAssemblyDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"27-04-2019\"},\"Unit\":\"$\"},{\"Name\":\"StockoutTransferLocation\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"StockinReturnDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"FabricatedLocation\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"StockinArrivalLocation\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"StockinTransferDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"Reference\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCIDENTIFIER\",\"value\":\"300 x 350\"},\"Unit\":\"$\"},{\"Name\":\"PlannedStockinArrivalDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"11-03-2019\"},\"Unit\":\"$\"},{\"Name\":\"PlannedFabricatedDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"01-03-2019\"},\"Unit\":\"$\"},{\"Name\":\"AssemblyDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"Slope\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCPLANEANGLEMEASURE\",\"value\":\"0\",\"orig_value\":\"0.\"},\"Unit\":\"$\"},{\"Name\":\"FabricatedDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"StockoutReturnLocation\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"Span\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCPOSITIVELENGTHMEASURE\",\"value\":\"4750\",\"orig_value\":\"4750.\"},\"Unit\":\"$\"},{\"Name\":\"StockinArrivalDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"StockoutDeliveryLocation\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"Status\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"Assembled\"},\"Unit\":\"$\"}]"
}
],
我有这个 JSONArray data
,其中只有一个对象。此对象中的 prop
元素包含一个类似于 Json 响应的字符串,如下所示。我如何遍历所有可能产生 JSONArray 的字符串?
我知道我得到的响应不是数组,但我无法控制它。
"data": [
{
"prop": "{\"Name\":\"StockoutTransferDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"LoadBearing\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCBOOLEAN\",\"value\":true},\"Unit\":\"$\"},{\"Name\":\"StockoutDeliveryDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"Status\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"OnTransport\"},\"Unit\":\"$\"},{\"Name\":\"Tracked\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"true\"},\"Unit\":\"$\"},{\"Name\":\"StockoutReturnDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"IsExternal\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCBOOLEAN\",\"value\":false},\"Unit\":\"$\"},{\"Name\":\"PlannedStockoutDeliveryDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"10-03-2019\"},\"Unit\":\"$\"},{\"Name\":\"StockinReturnLocation\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"PlannedAssemblyDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"27-04-2019\"},\"Unit\":\"$\"},{\"Name\":\"StockoutTransferLocation\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"StockinReturnDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"FabricatedLocation\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"StockinArrivalLocation\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"StockinTransferDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"Reference\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCIDENTIFIER\",\"value\":\"300 x 350\"},\"Unit\":\"$\"},{\"Name\":\"PlannedStockinArrivalDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"11-03-2019\"},\"Unit\":\"$\"},{\"Name\":\"PlannedFabricatedDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"01-03-2019\"},\"Unit\":\"$\"},{\"Name\":\"AssemblyDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"Slope\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCPLANEANGLEMEASURE\",\"value\":\"0\",\"orig_value\":\"0.\"},\"Unit\":\"$\"},{\"Name\":\"FabricatedDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"StockoutReturnLocation\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"Span\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCPOSITIVELENGTHMEASURE\",\"value\":\"4750\",\"orig_value\":\"4750.\"},\"Unit\":\"$\"},{\"Name\":\"StockinArrivalDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"StockoutDeliveryLocation\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"Status\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"Assembled\"},\"Unit\":\"$\"}"
}
],
我的代码片段是:
JSONArray jsonArray = response.getJSONArray("data");
JSONObject data = jsonArray.getJSONObject(0);
String prop = data.getString("prop");
JSONArray jsonArray1 = new JSONArray(prop);
我遇到错误 org.json.JSONException: Value {"Name":"StockTransferLocation","Description":"$","NominalValue":{"type":"IFCTEXT","value":"East"},"Unit":"$"} of type org.json.JSONObject cannot be converted to JSONArray
提前致谢!
只需要在String的开头和结尾加上一个方括号,然后尝试将其转换为JSONArray。
"data": [
{
"prop": "[{\"Name\":\"StockoutTransferDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"LoadBearing\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCBOOLEAN\",\"value\":true},\"Unit\":\"$\"},{\"Name\":\"StockoutDeliveryDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"Status\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"OnTransport\"},\"Unit\":\"$\"},{\"Name\":\"Tracked\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"true\"},\"Unit\":\"$\"},{\"Name\":\"StockoutReturnDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"IsExternal\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCBOOLEAN\",\"value\":false},\"Unit\":\"$\"},{\"Name\":\"PlannedStockoutDeliveryDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"10-03-2019\"},\"Unit\":\"$\"},{\"Name\":\"StockinReturnLocation\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"PlannedAssemblyDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"27-04-2019\"},\"Unit\":\"$\"},{\"Name\":\"StockoutTransferLocation\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"StockinReturnDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"FabricatedLocation\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"StockinArrivalLocation\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"StockinTransferDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"Reference\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCIDENTIFIER\",\"value\":\"300 x 350\"},\"Unit\":\"$\"},{\"Name\":\"PlannedStockinArrivalDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"11-03-2019\"},\"Unit\":\"$\"},{\"Name\":\"PlannedFabricatedDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"01-03-2019\"},\"Unit\":\"$\"},{\"Name\":\"AssemblyDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"Slope\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCPLANEANGLEMEASURE\",\"value\":\"0\",\"orig_value\":\"0.\"},\"Unit\":\"$\"},{\"Name\":\"FabricatedDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"StockoutReturnLocation\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"Span\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCPOSITIVELENGTHMEASURE\",\"value\":\"4750\",\"orig_value\":\"4750.\"},\"Unit\":\"$\"},{\"Name\":\"StockinArrivalDate\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"StockoutDeliveryLocation\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"\"},\"Unit\":\"$\"},{\"Name\":\"Status\",\"Description\":\"$\",\"NominalValue\":{\"type\":\"IFCTEXT\",\"value\":\"Assembled\"},\"Unit\":\"$\"}]"
}
],