使用 Javascript 将对象作为字符串导航以提取信息
Navigating object as string to extract information using Javascript
所以我需要从一个我无法控制的对象中提取某些数据,这些数据返回给我并且长度总是不同的。
我得到的格式化为字符串的响应如下:
{
"questionId": 18196101,
"externalQuestionId": "bcc38f7d30ea44ad908d8166dafa5556",
"category": "Employment Law",
"secondaryCategory": "",
"partnerName": "",
"questionTitle": "I sold my business but was offered a job with the company as",
"questionText": "I sold my business but was offered a job with the company as manager..."
}
所以基本上字符串是:'{"questionId":18196101,"externalQuestionId":"bcc38f7d30ea44ad908d8166dafa5556","category":"Employment Law","secondaryCategory":"","partnerName":"","questionTitle":"I sold my business but was offered a job with the company as","questionText":"I sold my business but was offered a job with the company as manager..."}'
现在我要说的是那些问题的全文,所以questionTitle
和questionText
我都知道。然后我可以在那个字符串中搜索它们的索引,我这样做是因为这个对象是非常小的一部分并且可以在任何更大响应的地方。
我需要从中提取的是 externalQuestionId
,或者在此示例中 "bcc38f7d30ea44ad908d8166dafa5556"
。
所有这些数字总是不同的,所以我必须依靠获取已知参数的索引。
所以基本上,我有那个字符串,我可以得到 questionTitle
的起始索引,我需要得到 externalQuestionId
数据。
例如,当我有 questionTitle
的索引时,如何获取最近的 externalQuestionId
的索引?
感谢 @nnnnnn 建议使用 .lastIndexOf()
。
我为我的特定案例创建了一个简单的函数,它获取我拥有的全文和片段以及 returns 我需要的数据。
function getQuestionId(fullText, textFragment) {
var newString = fullText.substr(fullText.lastIndexOf("externalQuestionId", fullText.indexOf(textFragment)));
return newString.substr(21, newString.indexOf(",") - 22);
}
var str = '{"questionId":18196101,"externalQuestionId":"bcc38f7d30ea44ad908d8166dafa5556","category":"Employment Law","secondaryCategory":"","partnerName":"","questionTitle":"I sold my business but was offered a job with the company as","questionText":"I sold my business but was offered a job with the company as manager..."}'
var frag = 'I sold my business but was offered a job with the company as","questionText":"I sold my business b';
console.log(getQuestionId(str, frag));
所以我需要从一个我无法控制的对象中提取某些数据,这些数据返回给我并且长度总是不同的。
我得到的格式化为字符串的响应如下:
{
"questionId": 18196101,
"externalQuestionId": "bcc38f7d30ea44ad908d8166dafa5556",
"category": "Employment Law",
"secondaryCategory": "",
"partnerName": "",
"questionTitle": "I sold my business but was offered a job with the company as",
"questionText": "I sold my business but was offered a job with the company as manager..."
}
所以基本上字符串是:'{"questionId":18196101,"externalQuestionId":"bcc38f7d30ea44ad908d8166dafa5556","category":"Employment Law","secondaryCategory":"","partnerName":"","questionTitle":"I sold my business but was offered a job with the company as","questionText":"I sold my business but was offered a job with the company as manager..."}'
现在我要说的是那些问题的全文,所以questionTitle
和questionText
我都知道。然后我可以在那个字符串中搜索它们的索引,我这样做是因为这个对象是非常小的一部分并且可以在任何更大响应的地方。
我需要从中提取的是 externalQuestionId
,或者在此示例中 "bcc38f7d30ea44ad908d8166dafa5556"
。
所有这些数字总是不同的,所以我必须依靠获取已知参数的索引。
所以基本上,我有那个字符串,我可以得到 questionTitle
的起始索引,我需要得到 externalQuestionId
数据。
例如,当我有 questionTitle
的索引时,如何获取最近的 externalQuestionId
的索引?
感谢 @nnnnnn 建议使用 .lastIndexOf()
。
我为我的特定案例创建了一个简单的函数,它获取我拥有的全文和片段以及 returns 我需要的数据。
function getQuestionId(fullText, textFragment) {
var newString = fullText.substr(fullText.lastIndexOf("externalQuestionId", fullText.indexOf(textFragment)));
return newString.substr(21, newString.indexOf(",") - 22);
}
var str = '{"questionId":18196101,"externalQuestionId":"bcc38f7d30ea44ad908d8166dafa5556","category":"Employment Law","secondaryCategory":"","partnerName":"","questionTitle":"I sold my business but was offered a job with the company as","questionText":"I sold my business but was offered a job with the company as manager..."}'
var frag = 'I sold my business but was offered a job with the company as","questionText":"I sold my business b';
console.log(getQuestionId(str, frag));