如何按对象的字段对对象的JSONArray进行排序?
输入:
[
{ "ID": "135", "Name": "Fargo Chan" },
{ "ID": "432", "Name": "Aaron Luke" },
{ "ID": "252", "Name": "Dilip Singh" }
];
Run Code Online (Sandbox Code Playgroud)
期望的输出(按"名称"字段排序):
[
{ "ID": "432", "Name": "Aaron Luke" },
{ "ID": "252", "Name": "Dilip Singh" }
{ "ID": "135", "Name": "Fargo Chan" },
];
Run Code Online (Sandbox Code Playgroud) 为了找出天气,JSON元素是JSONArray或JSONObject类型,我在位置0错误时得到意外的令牌END OF FILE.
我的JSON是:
{"colors":[{"color":"red","value":"#00"},{"color":"white","value":"#000g"}]}
Run Code Online (Sandbox Code Playgroud)
我的代码是:
java.io.FileReader reader = new java.io.FileReader("jsonpath");
org.json.simple.parser.JSONParser parser = new JSONParser();
System.Out.Println("aaaaaa JSON Class: "+parser.parse(reader).getClass());
if(parser.parse(reader) instanceof org.json.simple.JSONArray)
System.Out.Println("JSONArray");
else if(parser.parse(reader) instanceof org.json.simple.JSONObject)
System.Out.Println("JSONObject");
Run Code Online (Sandbox Code Playgroud)
当我运行上面的代码时,它会显示此输出
aaaaaa JSON Class: class org.json.simple.JSONObject Unexpected token END OF FILE at popsition 0
at org.json.simple.parser.JSONParser(Unknown Source)
.
.
.
<rest of the exception>
Run Code Online (Sandbox Code Playgroud)
我不明白为什么会发生这种异常.请帮帮我.
编辑后的更多细节:
我的这段代码与给定的json文件一起工作正常:
java.io.FileReader reader = new java.io.FileReader("jsonpath");
org.json.simple.parser.JSONParser parser = new JSONParser();
org.json.simple.JSONObject object = (JSONObject)parser.parse(reader);
System.Out.Println("JSONObject: "+object);
org.json.simple.JSONArray array = (JSONArray)object.get("colors"); …Run Code Online (Sandbox Code Playgroud) 我使用Spring卡夫卡第一次,我无法使用手动这里提到犯在我的消费者的代码Acknowledgement.acknowledge()方法https://docs.spring.io/spring-kafka/reference/html/_reference. html#commititting-offsets.我的是春季启动应用程序.如果我没有使用手动提交过程,那么我的代码工作正常.但是当我使用Acknowledgement.acknowledge()进行手动提交时,它会显示与bean相关的错误.此外,如果我没有正确使用手动提交,请建议我正确的方法.
错误信息:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field ack in Receiver required a bean of type 'org.springframework.kafka.support.Acknowledgment' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.kafka.support.Acknowledgment' in your configuration.
Run Code Online (Sandbox Code Playgroud)
我搜索了这个错误,我发现我需要添加@Component,但这已经存在于我的消费者代码中.
我的消费者代码如下所示:Receiver.java
import java.util.concurrent.CountDownLatch;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;
@Component
public class Receiver {
@Autowired
public Acknowledgment ack;
private CountDownLatch latch = new CountDownLatch(1);
@KafkaListener(topics = "${kafka.topic.TestTopic}")
public void receive(ConsumerRecord<?, ?> consumerRecord){
System.out.println(consumerRecord.value());
latch.countDown();
ack.acknowledge();
}
} …Run Code Online (Sandbox Code Playgroud)