我有一个Sinatra服务器,它以流方式从数据库返回多个JSON对象。对象看起来像:
{"a": 1, "b": 2, "c": 3}
{"a": 4, "b": 5, "c": 6}
...
但这是无效的JSON。我可以在Sinatra的事件处理中添加一个hack(手动注入缺少的数组分隔符),使响应看起来像:
[
{"a": 1, "b": 2, "c": 3}
, {"a": 4, "b": 5, "c": 6}
]
现在是有效的JSON,但这种技术不够优雅。有没有办法做这个客户端?基本上,我想要的是让一个JavaScript函数读取一个字符串并使用一个有效的JSON对象,然后返回给我JSON对象和字符串的其余部分,迭代地调用直到整个字符串被消耗。
答案 0 :(得分:14)
本机JSON.parse()
函数期望整个字符串是有效的JSON。我不知道一个解析器只消耗你想要的第一个有效对象。无论如何,人们应该真正生产有效的JSON。
如果您知道每行有一个对象,您可以使用split()
函数逐行拆分字符串并分别解析每一行。
var str = '{"a": 1, "b": 2, "c": 3}\n'+
'{"a": 4, "b": 5, "c": 6}';
var strLines = str.split("\n");
for (var i in strLines) {
var obj = JSON.parse(strLines[i]);
console.log(obj.a);
}
您还可以使用一些字符串操作将每一行转换为数组元素并解析整个元素。
str = "["+str.replace(/\n/g, ",")+"]";
JSON.parse(str);
答案 1 :(得分:2)
如果JSON字符串是单行,您可以执行以下操作:
var splitPoint = remainingData.indexOf("\n");
var currentJSONStr = splitPoint > -1 ? remainingData.substr(0, splitPoint) : remainingData;
remainingData = splitPoint > -1 ? remainingData.substr(splitPoint+1) : '';
var dataObj = youJSONDecodeFuncOrEval(currentJSONStr);
如果没有,请忽略我的回答。
我希望这可以帮助你,
阿林
注意:我试图满足要求
基本上,我想要的是拥有一个 JavaScript函数读取字符串和 使用有效的JSON对象,然后 返回给我JSON对象和 迭代地,字符串的剩余部分 被调用直到整个字符串 被消耗了。
这就是我没有使用.split("\n")
的原因。
答案 2 :(得分:2)
我会这样做:
var str = '{"a": 1, "b": 2, "c": 3}{"a": 4, "b": 5, "c": 6}';
var res = JSON.parse('[' + str.replace(/}{/g, '},{') + ']');
编辑:
作为tremby评论的重要内容
var str = '{"a": 1, "b": 2, "c": 3}{"a": 4, "b": 5, "c": 6}';
var res = JSON.parse('[' + str.replace(/}{(?=([^"]*"[^"]*")*[^"]*$)/g, '},{') + ']');
答案 3 :(得分:1)
这可能不是最有效的,但应该完成工作。
var s = '{"a": 1, "b": 2, "c": 3}{"a": 4, "b": 5, "c": 6}';
var sTemp = "";
var aObjs = [];
for(var i=0; i<s.length; ++i)
{
sTemp += s[i];
if (s[i] == "}")
{
aObjs.push(JSON.parse(sTemp));
sTemp = "";
}
}
如果您知道对象之间存在换行符,则会变得更加简单。
var sBad = '{"a": 1, "b": 2, "c": 3}\n{"a": 4, "b": 5, "c": 6}';
var sGood = "[" + sBad.replace(/\n/g, ",") + "]";
var aObjs = JSON.parse(sGood);
答案 4 :(得分:1)
o.string是json Object。
向对象数组或多个json对象添加一些像“new”这样的字符串。
例如:
json object----
{"id":2,"method":"listWirings","params":{"language":"anonymousLanguage","name":"mytest","working":"{\"modules\":[{\"config\":{\"position\":[186,59],\"xtype\":\"WireIt.ImageContainer\"},\"name\":\"Start\",\"value\":{}},{\"config\":{\"position\":[188,265],\"xtype\":\"WireIt.ImageContainer\"},\"name\":\"Stop\",\"value\":{}}],\"properties\":{\"description\":\"gfd\",\"name\":\"gf\"},\"wires\":[{\"src\":{\"moduleId\":0,\"terminal\":\"_OUTPUT\"},\"tgt\":{\"moduleId\":1,\"terminal\":\"StpIn\"}}]}"},"version":"json-rpc-2.0"}new
var str = o.toString();
var s = str.split("new");
for (var i = 0; i < s.length-1; i++)
{
var r = YAHOO.lang.JSON.parse(s[i]);
}
希望这将解析多个json对象。
答案 5 :(得分:0)
如果数据流在一行中为您提供多个JSON对象,则需要将它们分成一个数组:
const str = '{"a": 1, "b": 2, "c": 3}\n' +
'{"a": 4, "b": 5, "c": 6}' +
'{"a": 7, "b": 8, "c": 9}';
const json = '[' + str.replace(/}\n?{/g, '},{') + ']';
JSON.parse(json).forEach((obj) => {
console.log('a:', obj.a);
});
答案 6 :(得分:0)
我写了这个小JavaScript函数,它允许您将任何字符串解析为Json对象。它通过遍历每个字符并注意层次结构来工作。此解决方案的好处是,您可以获取文本的所有Json对象,而无需知道分隔它们的原因。
function evaluateJsonString(string){
var start = string.indexOf('{');
if(start == -1)
return false;
let hierarchy = 0;
let characters = string.split('');
let objects = [];
for(var index = start; index < characters.length; index++){
let char = characters[index];
if(char == '{')
hierarchy++;
if(char == '}')
hierarchy--;
if(hierarchy == 0){
objects.push(JSON.parse(characters.slice(start, index + 1).join('')));
index = start = index + characters.slice(index, characters.length).indexOf('{') - 1;
if(start == -1)
break;
}
}
return objects;
}
let result = evaluateJsonString('This is {"name": "John", "age": 32, "hobbies": ["sport", "programming"]} He goes to {"name": "University", "director": {"name": "Peter", "age": 66, "hobbies": ["drinking coffee"]}}');
console.log(result);
答案 7 :(得分:0)
我今天编写了一个小模块来执行此操作,并将其以json-multi-parse
的形式发布在NPM上。 code is available on Github。
我的解决方案很简单,但由于它依赖于解析这样的字符串时抛出的错误消息JSON.parse
,因此很容易变脆。它使用错误中给出的位置编号(“意外令牌{在JSON中的xyz中的意外标记中的编号”)解析之前的所有内容,然后递归解析之后的所有内容。
但是,它不会由于字符串中的花括号而中断,就像这里的其他一些建议解决方案一样。
这是该代码的简单版本,可在Chrome和Node中使用。
const ERROR_REGEX = /^Unexpected token { in JSON at position (\d+)$/;
function jsonMultiParse(input, acc = []) {
if (input.trim().length === 0) {
return acc;
}
try {
acc.push(JSON.parse(input));
return acc;
} catch (error) {
const match = error.message.match(ERROR_REGEX);
if (!match) {
throw error;
}
const index = parseInt(match[1], 10);
acc.push(JSON.parse(input.substr(0, index)));
return jsonMultiParse(input.substr(index), acc);
}
}
如果您也要支持Firefox,它将变得更加复杂,它以行号和该行中的字符的格式给出错误信息。我上面链接的模块可以处理这种情况。
答案 8 :(得分:-2)
我写了一个java转换器(使用jackson库),它将文件中的多个JSON对象转换为正确的JSON数组:
import java.io.File;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.MappingJsonFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
public class ParseJson {
ObjectMapper mapper = new ObjectMapper();
public static void main(String[] args) throws Exception {
File file = new File(args[0]);
JsonNode jn = new Parser().parse(file);
System.out.println(jn.toString());
}
private enum ParserState {
start,
object,
array,
field,
done
};
private static class Parser {
public Parser() {
}
public JsonNode parse(File file) throws Exception {
JsonNodeFactory factory = JsonNodeFactory.instance;
JsonFactory mappingFactory = new MappingJsonFactory();
@SuppressWarnings("deprecation")
JsonParser jp = mappingFactory.createJsonParser(file);
int n = 0;
JsonNode result = null;
JsonNode jn;
while((jn = parseNode(jp, false)) != null) {
if(n == 0) {
result = jn;
} else if(n == 1) {
ArrayNode an = factory.arrayNode();
an.add(result);
an.add(jn);
result = an;
} else if(n > 1) {
ArrayNode an = (ArrayNode)result;
an.add(jn);
} else {
throw new Exception("Unexpected parser state");
}
n++;
}
return result;
}
private JsonNode parseNode(JsonParser jp, boolean current) throws Exception {
JsonNodeFactory factory = JsonNodeFactory.instance;
ParserState state = ParserState.start;
JsonNode result = null;
String fieldName = null;
JsonToken token = current ? jp.getCurrentToken() : jp.nextToken();
for(; token != null; token = jp.nextToken()) {
// System.out.println("Token: "+token+": "+jp.getValueAsString());
switch(token) {
/**
* NOT_AVAILABLE can be returned if {@link JsonParser}
* implementation can not currently return the requested
* token (usually next one), or even if any will be
* available, but that may be able to determine this in
* future. This is the case with non-blocking parsers --
* they can not block to wait for more data to parse and
* must return something.
*/
case NOT_AVAILABLE: {
break;
}
/**
* START_OBJECT is returned when encountering '{'
* which signals starting of an Object value.
*/
case START_OBJECT: {
switch(state) {
case start: {
assert result == null;
assert fieldName == null;
result = factory.objectNode();
state = ParserState.object;
break;
}
case field: {
assert result != null;
assert fieldName != null;
ObjectNode on = (ObjectNode)result;
JsonNode jn = parseNode(jp, true);
on.set(fieldName, jn);
fieldName = null;
state = ParserState.object;
break;
}
case array: {
assert result != null;
assert fieldName == null;
ArrayNode an = (ArrayNode)result;
JsonNode jn = parseNode(jp, true);
an.add(jn);
break;
}
default: {
throw new Exception("Unexpected state: "+state+", for token: "+token);
}
}
break;
}
/**
* END_OBJECT is returned when encountering '}'
* which signals ending of an Object value
*/
case END_OBJECT: {
switch(state) {
case object: {
assert result != null;
assert fieldName == null;
state = ParserState.done;
break;
}
default: {
throw new Exception("Unexpected state: "+state+", for token: "+token);
}
}
break;
}
/**
* START_ARRAY is returned when encountering '['
* which signals starting of an Array value
*/
case START_ARRAY: {
switch(state) {
case start: {
assert result == null;
assert fieldName == null;
result = factory.arrayNode();
state = ParserState.array;
break;
}
case field: {
assert result != null;
assert fieldName != null;
ObjectNode on = (ObjectNode)result;
JsonNode jn = parseNode(jp, true);
on.set(fieldName, jn);
fieldName = null;
state = ParserState.object;
break;
}
case array: {
assert result != null;
assert fieldName == null;
ArrayNode an = (ArrayNode)result;
JsonNode jn = parseNode(jp, true);
an.add(jn);
break;
}
default: {
throw new Exception("Unexpected state: "+state+", for token: "+token);
}
}
break;
}
/**
* END_ARRAY is returned when encountering ']'
* which signals ending of an Array value
*/
case END_ARRAY: {
switch(state) {
case array: {
assert result != null;
assert fieldName == null;
state = ParserState.done;
break;
}
default: {
throw new Exception("Unexpected state: "+state+", for token: "+token);
}
}
break;
}
/**
* FIELD_NAME is returned when a String token is encountered
* as a field name (same lexical value, different function)
*/
case FIELD_NAME: {
fieldName = jp.getValueAsString();
switch(state) {
case object: {
assert result != null;
assert fieldName == null;
state = ParserState.field;
break;
}
default: {
throw new Exception("Unexpected state: "+state+", for token: "+token);
}
}
break;
}
/**
* Placeholder token returned when the input source has a concept
* of embedded Object that are not accessible as usual structure
* (of starting with {@link #START_OBJECT}, having values, ending with
* {@link #END_OBJECT}), but as "raw" objects.
*<p>
* Note: this token is never returned by regular JSON readers, but
* only by readers that expose other kinds of source (like
* <code>JsonNode</code>-based JSON trees, Maps, Lists and such).
*/
case VALUE_EMBEDDED_OBJECT: {
throw new Exception("Token not supported: "+token);
}
/**
* VALUE_STRING is returned when a String token is encountered
* in value context (array element, field value, or root-level
* stand-alone value)
*/
case VALUE_STRING: {
switch(state) {
case start: {
assert result == null;
assert fieldName == null;
result = factory.textNode(jp.getValueAsString());
state = ParserState.done;
break;
}
case field: {
assert result != null;
assert fieldName != null;
ObjectNode on = (ObjectNode)result;
JsonNode jn = factory.textNode(jp.getValueAsString());
on.set(fieldName, jn);
fieldName = null;
state = ParserState.object;
break;
}
case array: {
assert result != null;
assert fieldName == null;
ArrayNode an = (ArrayNode)result;
JsonNode jn = factory.textNode(jp.getValueAsString());
an.add(jn);
break;
}
default: {
throw new Exception("Unexpected state: "+state+", for token: "+token);
}
}
break;
}
/**
* VALUE_NUMBER_INT is returned when an integer numeric token is
* encountered in value context: that is, a number that does
* not have floating point or exponent marker in it (consists
* only of an optional sign, followed by one or more digits)
*/
case VALUE_NUMBER_INT: {
switch(state) {
case start: {
assert result == null;
assert fieldName == null;
result = factory.numberNode(jp.getLongValue());
state = ParserState.done;
break;
}
case field: {
assert result != null;
assert fieldName != null;
ObjectNode on = (ObjectNode)result;
JsonNode jn = factory.numberNode(jp.getLongValue());
on.set(fieldName, jn);
fieldName = null;
state = ParserState.object;
break;
}
case array: {
assert result != null;
assert fieldName == null;
ArrayNode an = (ArrayNode)result;
JsonNode jn = factory.numberNode(jp.getLongValue());
an.add(jn);
break;
}
default: {
throw new Exception("Unexpected state: "+state+", for token: "+token);
}
}
break;
}
/**
* VALUE_NUMBER_INT is returned when a numeric token other
* that is not an integer is encountered: that is, a number that does
* have floating point or exponent marker in it, in addition
* to one or more digits.
*/
case VALUE_NUMBER_FLOAT: {
switch(state) {
case start: {
assert result == null;
assert fieldName == null;
result = factory.numberNode(jp.getDoubleValue());
state = ParserState.done;
break;
}
case field: {
assert result != null;
assert fieldName != null;
ObjectNode on = (ObjectNode)result;
JsonNode jn = factory.numberNode(jp.getDoubleValue());
on.set(fieldName, jn);
fieldName = null;
state = ParserState.object;
break;
}
case array: {
assert result != null;
assert fieldName == null;
ArrayNode an = (ArrayNode)result;
JsonNode jn = factory.numberNode(jp.getDoubleValue());
an.add(jn);
break;
}
default: {
throw new Exception("Unexpected state: "+state+", for token: "+token);
}
}
break;
}
/**
* VALUE_TRUE is returned when encountering literal "true" in
* value context
*/
case VALUE_TRUE: {
switch(state) {
case start: {
assert result == null;
assert fieldName == null;
result = factory.booleanNode(true);
state = ParserState.done;
break;
}
case field: {
assert result != null;
assert fieldName != null;
ObjectNode on = (ObjectNode)result;
JsonNode jn = factory.booleanNode(true);
on.set(fieldName, jn);
fieldName = null;
state = ParserState.object;
break;
}
case array: {
assert result != null;
assert fieldName == null;
ArrayNode an = (ArrayNode)result;
JsonNode jn = factory.booleanNode(true);
an.add(jn);
break;
}
default: {
throw new Exception("Unexpected state: "+state+", for token: "+token);
}
}
break;
}
/**
* VALUE_FALSE is returned when encountering literal "false" in
* value context
*/
case VALUE_FALSE: {
switch(state) {
case start: {
assert result == null;
assert fieldName == null;
result = factory.booleanNode(false);
state = ParserState.done;
break;
}
case field: {
assert result != null;
assert fieldName != null;
ObjectNode on = (ObjectNode)result;
JsonNode jn = factory.booleanNode(false);
on.set(fieldName, jn);
fieldName = null;
state = ParserState.object;
break;
}
case array: {
assert result != null;
assert fieldName == null;
ArrayNode an = (ArrayNode)result;
JsonNode jn = factory.booleanNode(false);
an.add(jn);
break;
}
default: {
throw new Exception("Unexpected state: "+state+", for token: "+token);
}
}
break;
}
/**
* VALUE_NULL is returned when encountering literal "null" in
* value context
*/
case VALUE_NULL: {
switch(state) {
case start: {
assert result == null;
assert fieldName == null;
result = factory.nullNode();
state = ParserState.done;
break;
}
case field: {
assert result != null;
assert fieldName != null;
ObjectNode on = (ObjectNode)result;
JsonNode jn = factory.nullNode();
on.set(fieldName, jn);
fieldName = null;
state = ParserState.object;
break;
}
case array: {
assert result != null;
assert fieldName == null;
ArrayNode an = (ArrayNode)result;
JsonNode jn = factory.nullNode();
an.add(jn);
break;
}
default: {
throw new Exception("Unexpected state: "+state+", for token: "+token);
}
}
break;
}
default: {
throw new Exception("Token not supported: "+token);
}
}
if(state == ParserState.done) {
break;
}
}
return result;
}
}
}