我正在使用Spark 2.1.1(kafka 0.10+)阅读Kafka主题,有效负载是JSON字符串.我想用模式解析字符串并继续使用业务逻辑.
每个人似乎都建议我应该使用from_json解析JSON字符串,但是,它似乎不适合我的情况编译.错误是
not found : value from_json
.select(from_json($"json", txnSchema) as "data")
Run Code Online (Sandbox Code Playgroud)
当我尝试将以下几行放入火花壳时,它的效果很好 -
val df = stream
.select($"value" cast "string" as "json")
.select(from_json($"json", txnSchema) as "data")
.select("data.*")
Run Code Online (Sandbox Code Playgroud)
任何想法,我在代码中可能做错了,看到这个部分在shell中运行但在IDE /编译时没有?
这是代码:
import org.apache.spark.sql._
object Kafka10Cons3 extends App {
val spark = SparkSession
.builder
.appName(Util.getProperty("AppName"))
.master(Util.getProperty("spark.master"))
.getOrCreate
val stream = spark
.readStream
.format("kafka")
.option("kafka.bootstrap.servers", Util.getProperty("kafka10.broker"))
.option("subscribe", src_topic)
.load
val txnSchema = Util.getTxnStructure
val df = stream
.select($"value" cast "string" as "json")
.select(from_json($"json", txnSchema) as "data")
.select("data.*")
}
Run Code Online (Sandbox Code Playgroud) 我已经使用 Posion.decode 解析了以下 JSON!
json = %{"color-Black|size:10" =>
%{"attributes" => %{"color" => "Black","size" => "11"},
"isAvailable" => true,
"pricing" => %{"standard" => "$415.00", "sale" => 415}},
"color|size-EU:9.5" =>
%{"attributes" => %{"color" => "Black","size" => "11"},
"isAvailable" => true,
"pricing" => %{"standard" => "$415.00", "sale" => 415}}}
Run Code Online (Sandbox Code Playgroud)
我想映射它,但随着节点元素中的文本发生变化,我无法获取 JSON 元素。到目前为止我已经尝试过了。
Enum.map(json , fn(item) ->
%{
color: item.attributes["color"],
size: item.attributes["size"],
price: item.pricing["standard"] * 100,
isAvailable: item.isAvailable
}
end)
Run Code Online (Sandbox Code Playgroud)
但是这段代码给出了一些与访问相关的错误。
我创建了一个 Django Api。我使用了 rest_framework.generics.CreateAPIView 来发布。它在默认浏览器中运行良好。但是当我使用 Postman 时,它会引发错误。
views.py
class AuthorCreateAPIView(CreateAPIView):
queryset = Author.objects.all()
serializer_class = AuthorCreateUpdateSerializer
Run Code Online (Sandbox Code Playgroud)
serializers.py
class AuthorCreateUpdateSerializer(ModelSerializer):
class Meta:
model = Author
fields = [
'name',
'biography',
]
Run Code Online (Sandbox Code Playgroud)
错误:"detail": "JSON parse error - Expecting value: line 1 column 1 (char 0)"
我目前正在使用com.google.gson api(使用gson-2.8.5版本)的JsonObject和JsonParser来解析和读取输入JSON的值。
我有 JSON 归档,例如 smaple,"resultCode":"SUCCESS",当我尝试从 json 读取相同的值时,它给出的结果为""SUCCESS"".
我正在阅读的每个值都带有双“”,不知道为什么?您可以参考我的调试屏幕的下面屏幕。
我是 Json 和解析器的新手,这是默认行为吗?
我期待"SUCCESS","S"不像
我
在下图中突出显示的"00000000"那样""SUCCESS""。""S""""00000000""
请分享我们如何获得字符串的绝对值而不使用“””双引号字符串的任何想法,它会导致我的字符串比较失败。
String response_result = "{\"response\": {\"head\": {\"function\": \"acquiring.order.create\",\"version\": \"2.0\",\"clientId\": \"201810300000\",\"reqMsgId\": \"56805892035\",\"respTime\": \"2019-09-13T13:18:08+08:00\"},\"body\": {\"resultInfo\": {\"resultCode\": \"SUCCESS\",\"resultCodeId\": \"00000000\",\"resultStatus\": S,\"resultMsg\": \"SUCCESS\"},\"acquirementId\": \"2018080834569894848930\",\"merchantTransId\": \"5683668701112717398\",\"checkoutUrl\": \"http://localhost:8081/crm/operator/operator-search-init.action\"}},\"signature\":\"d+TUYLvt1a491R1e6aO8i9VwXWzVhfNgnhD0Du74f4RgBQ==\"}";
HttpInvoker.Result result = i.new Result(200, response_result);
JsonObject jo = new JsonParser().parse(response_result).getAsJsonObject();
String resultCode = jo.get("response").getAsJsonObject().get("body").getAsJsonObject().get("resultInfo").getAsJsonObject().get("resultCode").toString();
String resultCodeId = jo.get("response").getAsJsonObject().get("body").getAsJsonObject().get("resultInfo").getAsJsonObject().get("resultCodeId").toString();
String resultStatus = jo.get("response").getAsJsonObject().get("body").getAsJsonObject().get("resultInfo").getAsJsonObject().get("resultStatus").toString();
String checkoutUrl = jo.get("response").getAsJsonObject().get("body").getAsJsonObject().get("checkoutUrl").toString();
if ( RESULT_CODE_GCASH_SUCCESS.equals(resultCode) …Run Code Online (Sandbox Code Playgroud) 因此,我正在与 Github Actions 合作进行端到端测试。我正在查看的设置是让一项作业检索要测试的 url 列表,而我的第二项作业使用该列表创建一个矩阵并测试所有这些。我的问题是,当我实际运行测试脚本时,必须从命令行完成,因为我使用的是 Playwright。因此我不能直接使用我的矩阵对象;我必须将其输出到 JSON 文件。问题是,当我将 toJSON 输出到我的文件时,它会创建无效的漂亮打印 JSON,这会破坏我的脚本。这是我的代码:
name: <name>
on:
push:
workflow_dispatch:
jobs:
fetch_strategic_urls:
runs-on: ubuntu-latest
outputs:
urls: ${{ steps.req-urls.outputs.urls }}
steps:
- name: Request Urls
id: req-urls
run: |
export RESPONSE=$(
curl -X GET -H "Accept: application/json" <api-endpoint>)
echo "::set-output name=urls::$RESPONSE"
run_tests:
runs-on: ubuntu-latest
strategy:
matrix:
url: ${{needs.fetch_strategic_urls.outputs.urls}}
needs: fetch_strategic_urls
steps:
- ...
- ...
- run: |
ls
echo '${{ toJSON(matrix.url) }}' >> props.json
cat props.json
npm test
working-directory: E2E.Tests
Run Code Online (Sandbox Code Playgroud)
无论echo ${{matrix.url}} …
我是 Angular 和 Angular Material 的新手,
没有 Angular Materail 它工作正常
<table>
<tr>
<th>City</th>
<th>State</th>
</tr>
<tr>
<td>{{ varEmp?.address.city}}</td>
<td>{{ varEmp?.address.state}}</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
对于角度材质,它不起作用
<table mat-table [dataSource]="varEmp" class="mat-elevation-z8">
<!-- Position Column -->
<ng-container matColumnDef="city">
<th mat-header-cell *matHeaderCellDef> City</th>
<td mat-cell *matCellDef="let element"> {{element?.address.city}} </td>
</ng-container>
<!-- Name Column -->
<ng-container matColumnDef="state">
<th mat-header-cell *matHeaderCellDef> State</th>
<td mat-cell *matCellDef="let element"> {{element?.address.state}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
Run Code Online (Sandbox Code Playgroud)
在组件中我声明了变量
displayedColumns: string[] = ['city','state'];
Run Code Online (Sandbox Code Playgroud)
简要描述组件代码以降低复杂性
Json对象
{"address":
{"city": …Run Code Online (Sandbox Code Playgroud) 当前存在 PolymorphicJsonAdapterFactory 和 Kotlins 密封类的问题。我有一个返回多态主组件的 API,我试图在 Kotlin 中使用 Moshi 解析和创建多态对象,但出现以下错误:
Caused by: java.lang.IllegalArgumentException: Cannot serialize abstract class home.HomeComponent
for class home.HomeComponent
for java.util.List<home.HomeComponent> components
for class home.HomeContent
Run Code Online (Sandbox Code Playgroud)
代码:
sealed class HomeComponent(@Json(name = "_type") val type: HomeContentType) {
data class BannerComponent(@field:Json(name = "_id")
val id: String,
val image: String) : HomeComponent(HomeContentType.banner)
data class SpecialProductsComponent(@field:Json(name = "_id")
val id: String,
val style: List<Product>) : HomeComponent(HomeContentType.specialProducts)
data class CarouselBannerComponent(@field:Json(name = "_id")
val id: String,
val style: String,
val images: List<String>) : HomeComponent(HomeContentType.carousel)
} …Run Code Online (Sandbox Code Playgroud) 当我使用 MongoDB 2.xx 时,我曾经(BasicDBList) JSON.parse("[]")将字符串数据解析为 Document 数组。但是最新的 MongoDB 驱动程序说这已被弃用,BasicDbObject.parse("")唯一的转换为BasicDBObject.
这是我之前在 2.xx Java 驱动程序中使用的一些代码示例
BasicDbList skuList = (BasicDBList) JSON.parse(skus);
Run Code Online (Sandbox Code Playgroud)
所以当我升级到 3.6.1 时,编译器说这已被弃用。并建议使用BasicDbObject.parse()
但这只接受 JSON 对象结构化的字符串......
{ "fruit": "apple"}
Run Code Online (Sandbox Code Playgroud)
...不是 JSON 数组格式的字符串。
所以,如果我有一个像"[\"SKU000001\", \"SKU0000002\", \"SKU0000003\"]"我如何转换为的字符串
BasicDBList?
我期待我的应用程序中同时有 json 和 xml 响应。Retrofit 2.0 允许您针对此类情况添加多个转换器工厂。
但似乎顺序在这里至关重要。在 SimpleXmlConverterFactory 之上添加 JacksonConverterFactory 使得 Retrofit 仅接受 Json 响应,并在遇到 XML 时抛出异常,反之亦然。
下面是如何向您的 Retrofit Builder 添加多个 addConverterFactory 的代码片段。
.addConverterFactory(JacksonConverterFactory.create(objectMapper))
.addConverterFactory(SimpleXmlConverterFactory.create())
Run Code Online (Sandbox Code Playgroud)
<<<编辑
将上面的代码改成这样,还是不行:
return new Retrofit.Builder()
.client(clientBuilder.build())
.baseUrl(BuildConfig.API_ENDPOINT)
.addCallAdapterFactory(unAuthorizedHandlingCallAdapterFactory)
.addCallAdapterFactory(RxErrorHandlingCallAdapterFactory.create())
.addConverterFactory(new QualifiedTypeConverterFactory(JacksonConverterFactory.create(objectMapper), SimpleXmlConverterFactory.create()))
.build();
Run Code Online (Sandbox Code Playgroud)
编辑2
添加响应类型是关键 @GET("/") @Xml
尝试解析 JSON 响应并遇到此错误。
keyNotFound(CodingKeys(stringValue: "id", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "没有与键关联的值 CodingKeys(stringValue: "id", intValue: nil) ("id" ).",underlyingError: nil))
我并没有弄清楚为什么它首先要为 id 寻找 CodingKey,因为响应包含一个 id。
这是来自服务器的 JSON 响应:
{
"answer": {
"id": 6,
"title": "Here is the postman API answer",
"ownerId": 1
}
}
Run Code Online (Sandbox Code Playgroud)
这是我试图将其合并到的结构:
// MARK: - Answer
struct Answer: Codable {
let id: Int
let title: String
let ownerID: Int
let questionID, projectID: Int?
enum CodingKeys: String, CodingKey {
case id, title
case ownerID = "ownerId"
case questionID = "questionId" …Run Code Online (Sandbox Code Playgroud)